is (variable >> 32) undefined?

D

Davis King

I just discovered that sometimes on some compilers it is true that
(variable >> 32) == variable where variable is an unsigned long and on
these platforms an unsigned long is 32 bits. Is bit shifting a
variable in the manner undefined by the standard or is this just a bug
in the compiler?




examples:
in borland 5.5.1 the following code prints 1, I would expect it to be 0
unsigned long low = 1;
low >>= 32;
cout << low;

I also found a similar problem in visual studio but weather or not
(low>>32) == low or 0 is apparently dependent upon what (seeming
independent) statements precede the shift.
 
N

Nikolai Borissov

Davis King wrote :
I just discovered that sometimes on some compilers it is true that
(variable >> 32) == variable where variable is an unsigned long and on
these platforms an unsigned long is 32 bits. Is bit shifting a
variable in the manner undefined by the standard or is this just a bug
in the compiler?

I believe this is in compliance with the Standard. As I recall, both
negative and too large positive (when value goes beyond the size of a
variable) shifts produce undefined results.

Nikolai Borissov
 
J

Jack Klein

I just discovered that sometimes on some compilers it is true that
(variable >> 32) == variable where variable is an unsigned long and on
these platforms an unsigned long is 32 bits. Is bit shifting a
variable in the manner undefined by the standard or is this just a bug
in the compiler?




examples:
in borland 5.5.1 the following code prints 1, I would expect it to be 0
unsigned long low = 1;
low >>= 32;
cout << low;

I also found a similar problem in visual studio but weather or not
(low>>32) == low or 0 is apparently dependent upon what (seeming
independent) statements precede the shift.

If the shift count for an integer type is negative or greater than or
equal to the number of bits, the result is undefined. The compiler is
absolutely correct.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Ron Natalie

Davis King said:
I just discovered that sometimes on some compilers it is true that
(variable >> 32) == variable where variable is an unsigned long and on
these platforms an unsigned long is 32 bits. Is bit shifting a
variable in the manner undefined by the standard or is this just a bug
in the compiler?

It's undefined behavior if the number of bits in the shift is equal or greater
than the number of bits in the size of the other operand. I have worked
on one implementation where the above would be a no op rather than
a store of zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top