473,395 Members | 1,656 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,395 software developers and data experts.

Rotational bitwise shift

As I understand it, the unsigned long type is 4 bytes. Also, the bitwise shift (<<) is a zero-fill shift as opposed to a rotational shift. However when I shift, the value seems to be rotating, as opposed to being lost as I would expect.

Anybody have any ideas?


Expand|Select|Wrap|Line Numbers
  1. char resBuffer[sizeof(unsigned long)];
  2.  
  3. unsigned long value=1;
  4.  
  5. memcpy(resBuffer, &value, sizeof(unsigned long));
  6.  
  7.  
  8.  
Leaves resBuffer = { 1, 0, 0, 0 } as I would expect.


Expand|Select|Wrap|Line Numbers
  1. char resBuffer[sizeof(unsigned long)];
  2.  
  3. unsigned long value=1;
  4.  
  5. value = (value << 8);
  6.  
  7. memcpy(resBuffer, &value, sizeof(unsigned long));
  8.  
  9.  
  10.  
Leaves resBuffer = { 0, 1, 0, 0 } as I would expect.


Expand|Select|Wrap|Line Numbers
  1. char resBuffer[sizeof(unsigned long)];
  2.  
  3. unsigned long value=1;
  4.  
  5. value = (value << 36);
  6.  
  7. memcpy(resBuffer, &value, sizeof(unsigned long));
  8.  
  9.  
  10.  
However, leaves resBuffer = { 16, 0, 0, 0 }

Why isn't the result { 0, 0, 0, 0 } as I would expect?

Thanks for taking the time to read through this!
Oct 10 '06 #1
1 6645
Banfa
9,065 Expert Mod 8TB
I believe you may be running into undefined behaviour (I just can't actually find anyway that categorically states it) with

value = (value << 36);

because you are shifting more bits than is in an unsigned long (32) in this case.

If this is the case then having invoked undefined behaviour the compiler is free to do whatever it wants, on my system it looks like what it does is shift by (shift % 32), for 36 this results in a left shift by 4.

Anyway you should not rely on undefined behaviour because (being undefined) it can change by platform, compiler or even compiler version.
Oct 11 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
3
by: sandy_pt_in | last post by:
Hi C guru's can you please tell me how to do multiplication of 2 numbers using bitwize operators?? expecting reply.
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
8
by: Rudolf | last post by:
How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf
4
by: kernelxu | last post by:
Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This...
1
by: PengYu.UT | last post by:
I need to do rotational shift on bitset. But I couldn't find that operation in the standard. Is there any workaround or other packages to do this? Thanks, Peng
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.