C# UL and >> operators - c#

What does this meaning mean in words?
(SomeVariable * 330UL >> 10)
Is it: SomeVariable times 3.3 shift right 10 bit??

No.
It means SomeVariable times 330, promote to long and shift non-cyclically right 10bits.
(it would be cyclic, or arithmetic shift without the promotion).

Right-shifting an integral value by one is equivalent to dividing it by 2. Two shifts equivalent to dividing by 4. Etcetera. Which makes the expression equivalent to:
ulong value = ((ulong)SomeVariable * 330) / 1024;

UL stands for Unsigned Long. >> yes it is bitwise arithmetic shift.

SomeVariable times 330 as an unsigned long shift right 10 bits

Related

Right shift operator used for checking number negativity

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.

operator >> in C# [duplicate]

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).

c# calculation help (TExtractor callid)

I'm pretty useless when it comes to math and I have a problem I need help with.
This has nothing to do with schoolwork, it's in fact about alcatel and the ticketextractor. I have two values that needs to be calculated in a c# application according to a formula specified in their documentation:
"The global callid is equal to: callid1 multiplied by 2 power 32 plus callid2"
As I said I'm not big with maths so that statement says nothing to me. If anyone know how to calculate it i'd appreciate it! Thanks
First thing is you'll need a 64 bit value to store it in. Assuming your callId values are (32 bit) ints, you'd need to do something like this.
int callId1, callId2;
...
long globalCallId = ((long)callId1 << 32) + callId2;
<< is the bit shift operator - shifting 32 bits is the equivalent of multiplying by 2^32.
It's easiest to shift callid1 by 32 bits.
long globalCallId = ((long)callid1 << 32) + callid2;
So global callid = callid1 * 232 + callid2. You can use:
long globalCallID = (callid1 << 32) + callid2
This uses the fact that a << b == a multiplied by 2 to the power of b.

Fastest way to calculate an X-bit bitmask?

I have been trying to solve this problem for a while, but couldn't with just integer arithmetic and bitwise operators. However, I think its possible and it should be fairly easy. What am I missing?
The problem: to get an integer value of arbitrary length (this is not relevant to the problem) with it's X least significant bits sets to 1 and the rest to 0. For example, given the number 31, I need to get an integer value which equals 0x7FFFFFFF (31 least significant bits are 1 and the rest zeros).
Of course, using a loop OR-ing a shifted 1 to an integer X times will do the job. But that's not the solution I'm looking for. It should be more in the direction of (X << Y - 1), thus using no loops.
Try this: (1 << X) - 1
Try this:
uint.MaxValue >> (32 - something)
I think the following should work:
int mask = (int)Math.Pow(2, 31) - 1;
This is a single mathematical expression, but it isn't particularly efficient because calculating the power in this way is not really a good idea. However, since we're calculating a power of 2, we can do the same thing using shift:
int mask = (1 << 31) - 1;

Why arithmetic shift halfs a number only in SOME incidents?

Hey, I'm self-learning about bitwise, and I saw somewhere in the internet that arithmetic shift (>>) by one halfs a number. I wanted to test it:
44 >> 1 returns 22, ok
22 >> 1 returns 11, ok
11 >> 1 returns 5, and not 5.5, why?
Another Example:
255 >> 1 returns 127
127 >> 1 returns 63 and not 63.5, why?
Thanks.
The bit shift operator doesn't actually divide by 2. Instead, it moves the bits of the number to the right by the number of positions given on the right hand side. For example:
00101100 = 44
00010110 = 44 >> 1 = 22
Notice how the bits in the second line are the same as the line above, merely
shifted one place to the right. Now look at the second example:
00001011 = 11
00000101 = 11 >> 1 = 5
This is exactly the same operation as before. However, the result of 5 is due to the fact that the last bit is shifted to the right and disappears, creating the result 5. Because of this behavior, the right-shift operator will generally be equivalent to dividing by two and then throwing away any remainder or decimal portion.
11 in binary is 1011
11 >> 1
means you shift your binary representation to the right by one step.
1011 >> 1 = 101
Then you have 101 in binary which is 1*1 + 0*2 + 1*4 = 5.
If you had done 11 >> 2 you would have as a result 10 in binary i.e. 2 (1*2 + 0*1).
Shifting by 1 to the right transforms sum(A_i*2^i) [i=0..n] in sum(A_(i+1)*2^i) [i=0..n-1]
that's why if your number is even (i.e. A_0 = 0) it is divided by two. (sorry for the customised LateX syntax... :))
Binary has no concept of decimal numbers. It's returning the truncated (int) value.
11 = 1011 in binary. Shift to the right and you have 101, which is 5 in decimal.
Bit shifting is the same as division or multiplication by 2^n. In integer arithmetics the result gets rounded towards zero to an integer. In floating-point arithmetics bit shifting is not permitted.
Internally bit shifting, well, shifts bits, and the rounding simply means bits that fall off an edge simply getting removed (not that it would actually calculate the precise value and then round it). The new bits that appear on the opposite edge are always zeroes for the right hand side and for positive values. For negative values, one bits are appended on the left hand side, so that the value stays negative (see how two's complement works) and the arithmetic definition that I used still holds true.
In most statically-typed languages, the return type of the operation is e.g. "int". This precludes a fractional result, much like integer division.
(There are better answers about what's 'under the hood', but you don't need to understand those to grok the basics of the type system.)

Categories