0

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:

  1. Is it right?
  2. 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.

Community
  • 1
  • 1
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • Have you tried getting those answers by writing test code and then debugging it? That's an easy way to get yes/no answers from the language and framework. – Douglas Jan 04 '12 at 20:43
  • It's right, under conditions. I think that with negative numbers, >> doesn't give the same result as /. And you can use it with integers only, not with floats. – Mr Lister Jan 04 '12 at 20:45
  • 2
    If you aren't sure what bit-shifting does, you are sure what dividing does, and you're thinking about one as a replacement for the other, why not save yourself, future you, and future maintainer of your code (perhaps you, perhaps not you) the trouble and just divide? – Anthony Pegram Jan 04 '12 at 20:45
  • @Anthony true... and the compiler will optimize any performance differences away anyway. – Mr Lister Jan 04 '12 at 20:46
  • 1
    See the link in the article referenced in the duplicate. It describes when >> behaves like division, and when (more importantly) it doesn't... – James Michael Hare Jan 04 '12 at 20:46
  • 1
    My rule of thumb is use << and >> when you want to shift bits for bit manipulation, not when you want to perform an arithmetic trick, otherwise it becomes less maintainable. – James Michael Hare Jan 04 '12 at 20:47
  • @JamesMichaelHare another difference in behavior between `>>` and `/`, not mentioned in the linked article, is precedence. See http://stackoverflow.com/a/3060658/385844 – phoog Jan 06 '12 at 06:19

2 Answers2

1

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.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
1

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

S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58