Question about shifting return values

  • Thread starter Hannes Allmaier
  • Start date
H

Hannes Allmaier

Hi!

I found interesting code in a Visualc++-file (all variables are unsigned
integers)

return (CurrentBfr << (32 - BitsLeft)) >> (32 - N);;

so it seems the return value gets right shifted (32-N) times, like this

return(CurrentBfr << (32 - BitsLeft))>> (32 - N);

Interesting that this actually works, however even more interesting why
my simplification

return(CurrentBfr>>(Bitsleft-N));

doesn't work. If a left shift corresponds to a multiplication and a
right shift to a division by 2, this should be ok.

Could somebody enlighten me?

Thanks! Hannes
 
A

Alf P. Steinbach

* Hannes Allmaier:
I found interesting code in a Visualc++-file (all variables are unsigned
integers)

return (CurrentBfr << (32 - BitsLeft)) >> (32 - N);;

so it seems the return value gets right shifted (32-N) times, like this

return(CurrentBfr << (32 - BitsLeft))>> (32 - N);

The only difference is the null statement (the semicolon) in the first
example.

Interesting that this actually works, however even more interesting why
my simplification

return(CurrentBfr>>(Bitsleft-N));

doesn't work. If a left shift corresponds to a multiplication and a
right shift to a division by 2, this should be ok.

First, a shift operation discards information, the bits shifted out of
the value representation. For numbers that are small enough that
doesn't matter wrt. left shift n bits, because the bits shifted out then
don't change the numeric value from what you'd get by multiplying with
2^n. For larger numbers it does matter, so on that count alone your
simplification wouldn't work in all cases.

Second, the result of a C++ shift operation is undefined if the right
operand is negative, so 'x<<n' is not equivalent to 'x>>-n', which your
simplification relies on.
 
H

Hannes Allmaier

Thanks for your reply!

The only difference is the null statement (the semicolon) in the first
example.

Sorry, but what does that mean?

For larger numbers it does matter, so on that count alone your
simplification wouldn't work in all cases.

Yes you're right, but I'm dealing here with small numbers.
Second, the result of a C++ shift operation is undefined if the right
operand is negative, so 'x<<n' is not equivalent to 'x>>-n', which your
simplification relies on.

Sorry again, I forgot to mention that N<BitsLeft applies, so
(BitsLeft-N) is always positive.

Thanks a lot, Hannes
 
B

Ben Bacarisse

Thanks for your reply!



Sorry, but what does that mean?

You quoted two lines of code. One ended ";;" the other ";" but they were
otherwise identical. It looks odd that you quoted two (almost) identical
lines of code.
For larger numbers it does matter, so on that count alone your

Yes you're right, but I'm dealing here with small numbers.

How small are these numbers? Even with N < BitsLeft

(CurrentBfr << (32 - BitsLeft)) >> (32 - N)

is only equivalent to

CurrentBfr >> (Bitsleft - N)

for a very small set of values for CurrentBfr. Take N = 0 and BitsLeft
= 1 and the two expressions become:

(CurrentBfr << 31) >> 32 and CurrentBfr >> 1

If CurrentBfr is 16 bits (you did not give the size) then these are
equivalent for zero values. If 32-bit ints are used, then they are the
same for exactly two values. With 64-bit ints they are equivalent only
for 0..0x1ffffffff which is 0.000000047% of the possible values CurrentBfr
can take.

But either your ints are about 32 bits in size or your test values are
big enough to cause problems, because you say that your version does not
work. Does this helps you to see why?
 
H

Hannes Allmaier

Thanks a lot for all your help! I do understand now that these shifts
have their purpose.

It was just strange, because some code looks ehrm...ineffecient (deeply
nested ifs with gotos...).

Thanks and have a nice evening, Hannes
 
N

Nicholas D. Krempel

They do correspond to division and multiplication by 2, however your
simplification assumes infinitely many bits in the type.

Multiplying by 2 doesn't have an inverse in computer arithmetic - you lose
the information in the highest bit. Similarly dividing by 2 loses the lowest
bit.

Bit rotations are (much) nicer operations in this sense, but seemingly less
useful.

Nick Krempel
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top