Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is (variable >> 32) undefined?

Reply
Thread Tools

is (variable >> 32) undefined?

 
 
Davis King
Guest
Posts: n/a
 
      09-27-2003
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.

 
Reply With Quote
 
 
 
 
Nikolai Borissov
Guest
Posts: n/a
 
      09-27-2003
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


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      09-28-2003
On Sat, 27 Sep 2003 12:45:04 -0700, Davis King <>
wrote in comp.lang.c++:

> 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
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-29-2003

"Davis King" <> wrote in message news:bl4paj$86h$...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57