Hi Michael: It's pretty obvious that C++ and Objective-C echo the "mistake" of C for code-portability reasons. The motivation for propagating the "mistake" in the other four was most likely to avoid being too different from C.
I've been programming in C for 30 years, and still think it's weird that == binds more strongly than the bitwise operators. It would be easy for me to make the switch (to the precedence you are proposing). THH ________________________________________ From: Michael Ferguson [[email protected]] Sent: Thursday, January 02, 2014 3:01 PM To: chapel-developers Subject: [Chapel-developers] bitwise operator precedence Hi - I learned recently that even the creators of C (or at least, Dennis Ritchie) think that the bitwise operator precedence rules are wrong. See http://www.lysator.liu.se/c/dmr-on-or.html One way that this comes up is that if you are checking for particular bit, you have to write: if( (x & MASK) == MASK ) ... in other words, it would make more sense if & and other bitwise operations bind tighter than ==. I did a little survey of languages. The C way : C C++ Java Objective-C PHP C#, Javascript; in these languages, bitwise operations have lower precedence than ==. FORTRAN: bit ops not in language exactly GO: shift, bitwise and with multiply, bitwise or xor with + Erlang: bitwise and with *; all others bitwise ops with + Perl 6, Python, Ruby, Dart: all bitwise operators lower than + but before == I'm proposing that Chapel follow Go on this point, since shifting also means multiply by a power of 2. Some examples that I think work nicely this way: right-rotate: x >> 1 | x & 1 left-rotate: x << 1 | x >> 63 check mask set: if x & MASK == MASK check mask bit: if x & MASK != 0 // != 0 required because Chapel doesn't promote ints to bools append bits to number: x << 7 | new_bits Comments? Thanks, -michael P.S. Here are some precedence tables (from binds-tightest to loosest) for the various languages for the curious: GO (shift, and with multiply, or xor with +) * / % << >> & &^ + - | ^ == != < <= > >= && || Perl 6 (bitwise operators lower than + but before == ) ** ! + ~ etc unary operators * / % + - & | ^ != == etc && || (no shift operators) Erlang (bitwise and with *; all others with +) Unary + - bnot not / * div rem band and + - bor bxor bsl bsr or xor == etc Python (all bitwise ops lower than +, higher than ==) a[i], a.b ** + - ~ unary operators * / // % + - << >> & ^ | != == etc not x and or Ruby (all bitwise ops lower than +, higher than ==) a[i] ** ! ~ + - (unary) * / % + - >> << & ^ | <= > > >= == != (and others) && || not or and Dart (all bitwise ops lower than +, higher than ==) * / + - << >> & ^ | <= > <= < == != && || ------------------------------------------------------------------------------ Rapidly troubleshoot problems before they affect your business. Most IT organizations don't have a clear picture of how application performance affects their revenue. With AppDynamics, you get 100% visibility into your Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro! http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk _______________________________________________ Chapel-developers mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/chapel-developers ------------------------------------------------------------------------------ Rapidly troubleshoot problems before they affect your business. Most IT organizations don't have a clear picture of how application performance affects their revenue. With AppDynamics, you get 100% visibility into your Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro! http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk _______________________________________________ Chapel-developers mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/chapel-developers
