• 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

Understanding Shift Operators

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a liitle confused about how the shift operator works. I am following the Kathy and Bates book. Here are my doubts:

1) int x = 0x80000000;
System.out.println("Before shift occurs:" +x);
x = x << 1;
System.out.println("After shift occurs:" +x);

Answer: Before shift occurs -2147483648
After shift occurs 0

My question: What is that number 2147483648? Where did that come from? Please explain HOW and WHY they get that number.


2) Bitwise Complement Operator

int x = 5;
x = ~x;

Answer: ~x = -6

My question: 5 in binary is: 0101. Converting it gives: 1010 which is 10.
Why is the answer -6? Please explain.

I know these are very fundamental questions. Please excuse me.
Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start off by reading: Corey's TipLine on Shift Operators.

And this and some more.

Almost forgot: two's complement info

If you still need clarification come on back and ask
[ November 21, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
# 2) 5 in binary is 0101. Converted it give: 1010 which is: -8 + 0 + 2 + 0 = -6

waiting for the explaination of #1.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic