Re: About shift one number more than 32.



"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>


.



Relevant Pages

  • Re: bc: Dezimalpunkt auf dem Zehnerblock
    ... Num an, ohne Shift: Komma ... Num an, mit Shift: Punkt ... Ich jedenfalls brauche auch bei Zahleneingaben ...
    (de.comp.os.unix.shell)
  • Re: Simple encoding function
    ... I want this stuff to encode URLs, I have in database some numbers, ... sub fn { ... my $num = shift; ...
    (perl.beginners)
  • Re: compound conditional
    ... Bob wrote: ... sub whichbit {$num = shift; ...
    (comp.lang.perl.misc)
  • Re: How to print an array of char backward.
    ... But if a C programmer ... does not understand something x&1 or shift right/left then they have no ... because it expresses a general algorithm. ... while (num) { ...
    (comp.lang.c)
  • Re: Optimizing C
    ... occurring extra unnecessary cpu bandwidth would be nice. ... shift & rotate, anding, oring, shift til bit ... the first non zero bit from the left in sequence of 64 words. ... As for regular "shift & rotate, anding, oring", if your implementation ...
    (comp.lang.c)