简体   繁体   中英

Bitwise operations in C#

Is there a shorter and better-looking alternative to

(b == 0) ? 0 : 1;

in terms of bitwise operations?

Also, to get the correct sign (-1, 0 or 1) of a given integer a I am currently using

(a > 0) ? 1 : (a >> 32);

Are there any shorter (but not slower) ways?

Personally I'd stick with the first option for your "equal to zero or not" option.

For the sign of an integer, I'd use Math.Sign and assume that the JIT compiler is going to inline it - testing that assumption with benchmarks if it turns out to be a potential bottleneck.

Think about the readability above all - your first piece of code is blindingly obvious. Your second isn't. I'm not even convinced your second piece of code works ... I thought that right-shifts were effectively masked to the bottom 5 bits, assuming this is an Int32 ( int ).

EDIT: Just checked, and indeed your current second piece of code is equivalent to:

int y = x > 0 ? 1 : x;

The shift doesn't even end up in the compiled code.

Take this as an object lesson about caring more about micro-optimization than readability. It's much easier to make code work if it's easy to understand. If your code gives the wrong result, I don't care how quickly it runs.

Microoptimizations are the root of all evil . You sacrifice readability for a nanosecond. That's a bad bargain

in terms of bitwise operations... Can anyone please point that to me? Also, to get the correct sign (-1, 0 or 1) of a given integer a I am currently using

(a > 0) ? 1 : (a >> 32); 

Armen Tsirunyan and Jon Skeet answered your technical question, I am going to attempt to explain some technical misconceptions you appear to have.

The first mistake is that if you have a 32 bit signed integer and attempted to shift it by 32, you would be attempting to look at the 33rd bit which in the case of a signed base 2 arthmetic would be an overflow bit.

The second mistake is when you have 32-bit signed binary value. The last bit would either be a one or zero. There is a only a single zero value. So your statement of trying to figure out if the sign is ( -1,0,1) clearly indicates you don't understand this fact. If the signed bit is a 1 the number would be negative, if it was zero, it would be positive. The structures that handle a number for the most part within the .NET Framework are not aware of 2's complement and 1's complement. This of course does not mean you cannot extend that functionality or simply convert a signed integer to a 2's complement number ( really simple honestly ).

I should add that there is only one value for zero when you have a signed integer. I guess this is was my major problem with your "check the sign" statement that you made which shows a misconception about binary numbers.

http://en.wikipedia.org/wiki/Signed_magnitude#Sign-and-magnitude

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM