| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Hannes Allmaier" |
| Date: |
03 Jul 2005 11:07:00 AM |
| Object: |
Question about shifting return values |
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
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Question about shifting return values |
03 Jul 2005 11:22:29 AM |
|
|
* 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
| User: "Hannes Allmaier" |
|
| Title: Re: Question about shifting return values |
03 Jul 2005 11:45:50 AM |
|
|
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
.
|
|
|
| User: "Ben Bacarisse" |
|
| Title: Re: Question about shifting return values |
03 Jul 2005 12:54:03 PM |
|
|
On Sun, 03 Jul 2005 18:45:50 +0200, Hannes Allmaier wrote:
Thanks for your reply!
The only difference is the null statement (the semicolon) in the first
example.
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
simplification wouldn't work in all cases.
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?
--
Ben.
.
|
|
|
| User: "Hannes Allmaier" |
|
| Title: Re: Question about shifting return values |
03 Jul 2005 01:24:54 PM |
|
|
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
.
|
|
|
|
|
|
|
| User: "Nicholas D. Krempel" |
|
| Title: Re: Question about shifting return values |
08 Jul 2005 06:32:43 PM |
|
|
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
"Hannes Allmaier" <allmaier@gmx.net> wrote in message
news:e087d$42c80d2a$d4bacef0$26118@news.chello.at...
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
.
|
|
|
|

|
Related Articles |
|
|