Question about shifting return values 
July 23rd, 2005, 06:49 AM
| | | |
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 | 
July 23rd, 2005, 06:49 AM
| | | | re: Question about shifting return values
* Hannes Allmaier:[color=blue]
>
> 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);[/color]
The only difference is the null statement (the semicolon) in the first
example.
[color=blue]
> 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.[/color]
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? | 
July 23rd, 2005, 06:49 AM
| | | | re: Question about shifting return values
Thanks for your reply!
[color=blue]
> The only difference is the null statement (the semicolon) in the first
> example.[/color]
Sorry, but what does that mean?
For larger numbers it does matter, so on that count alone your[color=blue]
> simplification wouldn't work in all cases.[/color]
Yes you're right, but I'm dealing here with small numbers.
[color=blue]
> 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.[/color]
Sorry again, I forgot to mention that N<BitsLeft applies, so
(BitsLeft-N) is always positive.
Thanks a lot, Hannes | 
July 23rd, 2005, 06:49 AM
| | | | re: Question about shifting return values
On Sun, 03 Jul 2005 18:45:50 +0200, Hannes Allmaier wrote:
[color=blue]
> Thanks for your reply!
>
>[color=green]
>> The only difference is the null statement (the semicolon) in the first
>> example.[/color]
>
> Sorry, but what does that mean?[/color]
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.
[color=blue]
> For larger numbers it does matter, so on that count alone your[color=green]
>> simplification wouldn't work in all cases.[/color]
>
> Yes you're right, but I'm dealing here with small numbers.[/color]
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. | 
July 23rd, 2005, 06:49 AM
| | | | re: Question about shifting return values
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 | 
July 23rd, 2005, 06:53 AM
| | | | re: Question about shifting return values
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. ..[color=blue]
> 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[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|