Board index » cppbuilder » Shift left operator

Shift left operator


2007-09-12 09:40:47 PM
cppbuilder19
I'd like to know what say the standard about shift left operatot then count
is equal or greater then number bits in a data. And is there any difference
in behavior between C and C++.
Is it correct to write (in C++ and C)?
1 << ( sizeof( int ) * 8 )
Vladimir Grigoriev
 
 

Re:Shift left operator

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
Quote
I'd like to know what say the standard about shift left operatot then
count is equal or greater then number bits in a data. And is there any
difference
result is undefined
Quote
in behavior between C and C++.
there is no difference
Quote
Is it correct to write (in C++ and C)?

1 << ( sizeof( int ) * 8 )
no
IMHO it is better to use CHAR_BIT instead of 8
Cheers,
Serge
 

Re:Shift left operator

Thanks.
"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
Quote

"Vladimir Grigoriev" < XXXX@XXXXX.COM >wrote in message
news:46e7eb77$ XXXX@XXXXX.COM ...
>I'd like to know what say the standard about shift left operatot then
>count is equal or greater then number bits in a data. And is there any
>difference

result is undefined

>in behavior between C and C++.

there is no difference

>Is it correct to write (in C++ and C)?
>
>1 << ( sizeof( int ) * 8 )

no

IMHO it is better to use CHAR_BIT instead of 8

Cheers,
Serge






 

{smallsort}

Re:Shift left operator

Hi, Serge,
Quote
>I'd like to know what say the standard about shift left operatot then
>count is equal or greater then number bits in a data.

result is undefined

Is it realy so?
I always thought, that the result of i<<n is the shifted bit pattern, where
the n leftmost bits are just lost, and the n rightmost bits a zero. Hence if
n>sizeof(int)*8, then the result is just zero. I often assumed it in my
programs. Is it a mistake? Or it depends on the compiler?
Thank you,
Vladimir
 

Re:Shift left operator

"Vladimir Shvetsov" < XXXX@XXXXX.COM >wrote in message
Quote
>>I'd like to know what say the standard about shift left operatot then
>>count is equal or greater then number bits in a data.
>
>result is undefined
>
Is it realy so?
5.8 Shift operators
1 ... The behavior is undefined if the right operand is negative,
or greater than or equal to the length in bits of the promoted
left operand.
Quote
I always thought, that the result of i<<n is the shifted bit pattern,
where the n leftmost bits are just lost, and the n rightmost bits a zero.
Hence if n>sizeof(int)*8, then the result is just zero. I often assumed
it in my programs. Is it a mistake? Or it depends on the compiler?
No, it may be not a mistake. You may know specifics of your compiler
and use them to write your code.
But ... that means that you have to investigate this issue for every
compiler
(and compiler options) you going to use for compiling your code (and do
that for all upgrades), write some compiler specific logic if they behave
differently; and document which guarantees you are using, why you doing
that, and what you gain.
Cheers,
Serge
 

Re:Shift left operator

Sergiy Kanilo wrote:
Quote
"Vladimir Shvetsov" < XXXX@XXXXX.COM >wrote in message

5.8 Shift operators
1 ... The behavior is undefined if the right operand is negative,
or greater than or equal to the length in bits of the promoted
left operand.

>I always thought, that the result of i<<n is the shifted bit pattern,
>where the n leftmost bits are just lost, and the n rightmost bits a zero.
>Hence if n>sizeof(int)*8, then the result is just zero. I often assumed
>it in my programs. Is it a mistake? Or it depends on the compiler?
I know for a fact that, using BCB2006 or CG2007, if you do something like
unsigned n = 32;
unsigned r = 1 << n;
the result, r, is 1 not zero. I was trying to compute bit masks for
data acquisition hardware. The boards I use have 16, 24, or 32 bit
counters, so I wrote
unsigned mask = (1ul << bits) - 1;
which fails miserably for bits = 32. I ended up writing,
unsigned mask = ((1ul << (bits-1)) << 1) - 1;
The underlying cause is the i386 architecture. The description for the
i386 SAL / SHL (shift left arithmetic / logical) instructions state that
the shift count is masked to 5 bits. Thus the shift is actually n
modulo 32. [Crawfird & Gelsinger, Programming the 80386, Sybex, 1987]
-Eliot
 

Re:Shift left operator

Vladimir Shvetsov wrote:
Quote
I always thought, that the result of i<<n is the shifted bit pattern, where
the n leftmost bits are just lost, and the n rightmost bits a zero. Hence if
n>sizeof(int)*8, then the result is just zero. I often assumed it in my
programs. Is it a mistake? Or it depends on the compiler?
Depends on the CPU.
Look at the documentation for SHL.
(for 32bit operands) "the count is masked to 5 bits"
So, SHL EAX, 40 results in an 8 bit shift
 

Re:Shift left operator

Eliot Frank < XXXX@XXXXX.COM >wrote:
Quote
I wrote
unsigned mask = (1ul << bits) - 1;
which fails miserably for bits = 32. I ended up writing,
unsigned mask = ((1ul << (bits-1)) << 1) - 1;
How about -1UL>>(32 - bits)