Jump to content
  • Advertisement
Sign in to follow this  
_moagstar_

C# Bitshift

This topic is 3322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've decided to have a go at some C# programming, and I'm trying to do the following : ulong bitshift = (8 * vertical) + horizontal; return 1uL << bitshift; However I am getting the following error : Operator '<<' cannot be applied to operands of type 'ulong' and 'ulong' Can anyone explain why this operation is not allowed please?

Share this post


Link to post
Share on other sites
Advertisement
Out of curiosity how does C#, Java and the other managed and/or "portable" languages handle out-of-range or negative shift counts?
In C/C++ they do whatever the processor does natively of course, so typically mod 32/64 shifts I suppose.

Share this post


Link to post
Share on other sites
In C#, if the first operand is signed, then the second operand is anded with 0x1F and the first operand is shifted that many places. If the first operand is unsigned, then the second operand is anded with 0x3F and the first operand is shifted that many places. I don't know how Java handles it.

Share this post


Link to post
Share on other sites
Python (at least up to 2.6; P3K may do something different) raises a ValueError if the shift is negative, and an OverflowError if the shift amount is too large to fit in an int. However, it does not care if you shift by 32 or more bits; the result is simply promoted to 'long' (which in Python is actually a bignum type).

'1 << (1 << 31 - 1)' is a good way to hang up the Python prompt ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!