473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 23 '05 #1
5 1261
* 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?
Jul 23 '05 #2
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
Jul 23 '05 #3
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.

Jul 23 '05 #4
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
Jul 23 '05 #5
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" <al******@gmx.net> wrote in message
news:e0***************************@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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4376
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting?...
5
4071
by: Hong Kong Phooey | last post by:
I wrote a control in which I overrode WndProc to implement a custom MouseMove. I caught the MouseMove message and passed it to a function which raises a custom event. In this function I get the x...
10
10662
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
12
2229
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.