I am confused why does this produce 0xffffffff in C#, I was expecting it to produce 0x0. The type of the expression is uint.
Console.WriteLine(0xffffffff >> 32);
As per the documentation:
If the first operand is an int or uint (32-bit quantity), the shift
count is given by the low-order five bits of the second operand
(second operand & 0x1f).
The second operand is 32. 32 & 0x1f is 0. Thus >> 32 is equivalent to 'shift this by 0 bits' therefore 'do nothing at all'.
Related
Let's say I wrote the following :
Console.WriteLine("{0:X8}", (uint)1 << 31);
It returns 80000000 (as expected).
However, If I wrote :
Console.WriteLine("{0:X8}", (uint)1 << 32);
It returns 00000001.
I would expect the "1" bit to be discarded and the result to be 00000000.
Here is what documentation says :
The left-shift operation discards the high-order bits that are outside
the range of the result type and sets the low-order empty bit
positions to zero.
Indeed, if I wrote this :
Console.WriteLine("{0:X8}", (uint)0xFA << 28);
It returns A0000000 (F is discarded)
From that same documentation page:
For the x << count and x >> count expressions, the actual shift count depends on the type of x as follows:
If the type of x is int or uint, the shift count is defined by the low-order five bits of the right-hand operand. That is, the shift count is computed from count & 0x1F (or count & 0b_1_1111).
32 & 0x1F is 0.
This "gotcha" is bad enough that former C# design team member Eric Lippert named it the 8th worst C# feature.
I am trying to use the << operator on a long, like so:
((long) num3) << ( 2 + (long) num4)))
This gives me the following error:
Operator << cannot be applied to operands of type long and long.
If I don't cast num4 to a long, there is no error. However, I cannot keep it as an int. Is there any other way around this?
The right operand has to be an int, not a long. It wouldn't make sense to use a long as the number of bits to shift, since integral types in C# never have more than 64 bits.
I am trying to check of a number is negative without using comparison operators. I am trying to examine the most significant bit. I am doing the following :
int x = -5;
Console.WriteLine(x >> 31);
I was expecting the output to be 1. But I get -1. What explains this behavior?
That's because operator >> on an int behaves as an arithmetic shift: the bits that are "pushed in" take the value of the sign bit (either 0 or 1, 1 in your case). So the resulting number you get is 0xFFFFFFFF, that is -1.
From MSDN:
If the first operand is an int or long, the right-shift is an
arithmetic shift (high-order empty bits are set to the sign bit). If
the first operand is of type uint or ulong, the right-shift is a
logical shift (high-order bits are zero-filled).
If you do:
Console.WriteLine((uint)x >> 31);
This is a logical shift, the higher bits are filled with 0, and the output is the one you expected: 1.
I have a 16 bit signed number coming in from hardware. I want to caste it into an Int32.
When I cast it as a short, it works occasionally when the number is negative. Most of the time however, I get a first chance exception of type 'System.OverflowException' occurred.
Here is my code:
int M1;
M1 = (short)(INBuffer[3] << 8) + INBuffer[2];
How do I cast a 16 bit short to a 32 bit integer in C#?
Assuming INBuffer is a byte array, you can safely cast to a ushort but not a short. This is because if the highest bit of the higher order byte is 1, the value is too large for a signed short once it is bitshifted.
In your case, if you want an int, no need to cast at all - the bit shift outputs an int, and the addition of a byte again leaves an int - you're already there...
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does the “>>” operator in C# do?
Also related: Doubling a number - shift left vs. multiplication
This is code in C#.
I just need to know if I'm right.
I'm not sure with using operator >>.
this code:
winHalfWidth = this.ClientSize.Width >> 1; // both is int
make the same as:
winHalfWidth = this.ClientSize.Width / 2;
and if I'm right so, this code should do:
quaterWidht = this.ClientSize.Width >> 2;
should do same as:
quaterWidth = this.ClientSize.Width / 4;
So 2 questions:
Is it right?
Is any difference between using >> and /?
When we speak about int values?
When we speak abou float values? (I thing there could be.. but I'am not sure.)
Answers like yes/no would be nice :)
Thanks.
It does in fact do what you intend with int values, but you should be careful about edge cases such as negative numbers or very big numbers.
As for floating-point numbers, their memory representation is not that simple, so a simple shift-right won't do the trick of dividing by 2.
This is shift operator not divide , we know that shift left will multiply a number by 2 , For exmaple
2 in base 2 is : 0010 , if we shift it one times to left it will turn to 4 or 0100.
So as MSDN said :
If the first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of the second operand (second operand & 0x1f).
If the first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of the second operand (second operand & 0x3f).
If the first operand is an int or long, the right-shift is an arithmetic shift (high-order empty bits are set to the sign bit). If the first operand is of type uint or ulong, the right-shift is a logical shift (high-order bits are zero-filled).