473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Bit shift question.

I am seeing a discrepancy on how bit shifts work in C# vs. C++. In C++, if
you do a bit shift and it overflows, the bits are lost.

i.e. in C++, -1 << 32 would produce 0. However, in C#, this same expression
yields -1.
Is there anyway to have C# bit shift behave like C++?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
May 31 '06 #1
3 3514
> Is there anyway to have C# bit shift behave like C++?

Well, actually, it is. Sorta. Bit's shifted off the left ARE lost.
print -1 << 31 and you'll see that. The problem is that C# since
shifting a 32-bit number 32 bits is undefined, C# executes "a << b" as
if it were written "a << (b & 31)".

Ken Varn wrote:
I am seeing a discrepancy on how bit shifts work in C# vs. C++. In C++, if
you do a bit shift and it overflows, the bits are lost.

i.e. in C++, -1 << 32 would produce 0. However, in C#, this same expression
yields -1.


May 31 '06 #2
Bitshifting by more than (n bits-1) is unspecified in c++ standard, so
different compilers may implement it in different ways and different
platforms may react different on this.

So you have to make sure that your shift count lies in the range of 0 to 31.

Iam not sure, but using a cast to uint may solve your problem in C#, though.

--

"Ken Varn" <nospam> schrieb im Newsbeitrag
news:OF**************@TK2MSFTNGP05.phx.gbl...
I am seeing a discrepancy on how bit shifts work in C# vs. C++. In C++, if
you do a bit shift and it overflows, the bits are lost.

i.e. in C++, -1 << 32 would produce 0. However, in C#, this same
expression
yields -1.
Is there anyway to have C# bit shift behave like C++?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

May 31 '06 #3
ja**********@gmail.com wrote:
Is there anyway to have C# bit shift behave like C++?
Well, actually, it is. Sorta. Bit's shifted off the left ARE lost.
print -1 << 31 and you'll see that. The problem is that C# since
shifting a 32-bit number 32 bits is undefined, C# executes "a << b" as
if it were written "a << (b & 31)".


And if compiler recognises that the number of steps would be zero, the
shift is optimized away. "-1 << 32" executes as if it were written "-1".

:)
Ken Varn wrote:
I am seeing a discrepancy on how bit shifts work in C# vs. C++. In C++, if
you do a bit shift and it overflows, the bits are lost.

i.e. in C++, -1 << 32 would produce 0. However, in C#, this same expression
yields -1.

May 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Antioch | last post by:
Ok, so Im a newb python programmer and I'm trying to create a simple python web-application. The program is simply going to read in pairs of words, parse them into a dictionary file, then randomly...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
11
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
13
by: HARDCORECODER | last post by:
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that...
1
by: jonathanmcdougall | last post by:
I am developing a calendar on which dates can be clicked and selected. A calendar is a table in which each day is a cell. Each cell has a unique id. By left-clicking on a date, it gets selected...
7
by: gokkog | last post by:
Hello, Recently I have the book Programming Pearls. Nice read! Perhaps it is weekend, I cannot understand the following codes well: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F...
3
by: Amy Smith | last post by:
Hello there, I am having a small problem which been challenging me for few days and need help or advice. I am trying to calculate the day-shift for employees based on the time they started and...
6
by: saumya.agarwal | last post by:
Hi, I am using libxml2 for xml parsing. When the client application sends data to libxml2 in UTF-8 format, it works fine. But, I have a scenarion in which the client application sends data to...
4
by: hui11 | last post by:
Hi, According to the doc at mozilla, http://developer.mozilla.org/en/docs/DOM:event.charCode, the charCode takes shift into consideration when pressed. I found that to be true for other cases...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.