• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bit shifting too much

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you bit shift too far?
For ex:
char c = 0xffff;
c >>= 16; // 1 right?
what about ...
c >>= 17; // ???
Thanks!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nate Johnson:
For ex:
char c = 0xffff;
c >>= 16; // 1 right?


No, c >> 16 will give you 0, not 1. c >> 15 will give you 1.
Knowing that, can you answer your own question? If not, let me know and I'll explain.
Corey
 
Nate Johnson
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

No, c >> 16 will give you 0, not 1. c >> 15 will give you 1.
Knowing that, can you answer your own question? If not, let me know and I'll explain.
Corey


I see that c >> 16 would be zero because it shifts 16 0's into the variable. No room left for anything else... now if you shift 17, does the compiler care that there really arent 17 bits in a char, or does it just keep pushing 0's making the answer zero?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nate Johnson:

or does it just keep pushing 0's making the answer zero?


You got it. When you do a signed right shift (as you're doing here), the operation performed is to continue adding sign bits to the left (in this case, 0's) and dropping off whatever is on the right. For example:

This will always print out 0, no matter what value you use for x.
I hope that helps,
Corey
 
Nate Johnson
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

This will always print out 0, no matter what value you use for x.
I hope that helps,
Corey



That's helps a lot! Thanks. What about a left shift? Left shift always pushes a zero on the right end and you lose the high-most bit... so if you left shift enough, you will end up with zero too? Or will there be some kind of "Type of of bounds exception"? for trying to make the number too big?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nate Johnson:
What about a left shift? Left shift always pushes a zero on the right end and you lose the high-most bit... so if you left shift enough, you will end up with zero too?


Usually. This can be a little tricky. When you're performing a shift, if you try to shift (in a single shift) by the size of the variable or more, you'll really end up shifting by your shift distance mod the variable size. For example:

However, if you do the shift in two smaller increments, they'll work as you'd probably expect.
Check on this code for a minute (along with its output) and see if you can follow what's going on. As usual, if you're still confused, just ask.

Corey
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The reason you're adding 0s for the signed right shift is because the char value is an unsigned integer value, am I right?

Clement
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
will u plz explain for me why the output of
i<<31 is a negave number with this value.

Originally posted by Corey McGlone:

Corey

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Clement Ng:
The reason you're adding 0s for the signed right shift is because the char value is an unsigned integer value, am I right?


Yes, that is correct. Had you done a right shift to a signed, negative number, you'd be shifting 1's in from the left, rather than 0's.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vinita Kh:
Corey,
will u plz explain for me why the output of
i<<31 is a negave number with this value.


Remember that the left-most bit is a sign bit. Think about what the integer 1 looks like in binary:

Now, shift that left 31...

You're left with the representation for the most negative number an int can hold. You see, the 1 (that was a data bit) is now shifted into the sign bit position. Things like this can dramatically affect your applications.
I hope that helps,
Corey
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

Remember that the left-most bit is a sign bit.


Because the primitive types byte, short, int, and long are represented in two's complement?
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Corey.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Val Pecaoco:

Because the primitive types byte, short, int, and long are represented in two's complement?


Yes.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic