Re: About shift one number more than 32.
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Thu, 31 May 2007 11:27:11 -0400
"L.G" wrote:
I met one problem of shift a number with equal or more than 32 bits.
When running this :
uint32_t num = 0x45F5F;
int shift = 33;
uint32_t res = num << shift;
printf( "\\> exp: %08XH << %d = %08XH\n", num, shift, res );
It shows :
\> exp: 00045F5FH << 33 = 0008BEBEH
Is it personal ?
My environment :
WinXP SP2 + Cygwin 2.510.2.2
My understanding is that shifting by more that the width of the
item is either "implementation defined" or UB. In this case,
you are asking to shift a 32-bit integer by 33 bits.
My guess, based on experience with x86 hardware, is that the CPU
only uses the low-order 5 bits of the shift value (2^5 == 32),
so a shift of 33 (0x21) is treated the same as a shift of 1
(0x21 & 0x1f == 0x01).
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- References:
- About shift one number more than 32.
- From: L.G
- About shift one number more than 32.
- Prev by Date: Re: small C compiler that will run on Vista
- Next by Date: Re: How to print in C
- Previous by thread: Re: About shift one number more than 32.
- Next by thread: Time and date
- Index(es):
Relevant Pages
|