A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Understanding Shift Operators
Joined: Oct 29, 2004
Nov 21, 2004 11:47:00
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.
Joined: Aug 03, 2002
Nov 21, 2004 12:08:00
Start off by reading:
Corey's TipLine on Shift Operators
two's complement info
If you still need clarification come on back and ask
[ November 21, 2004: Message edited by: Barry Gaunt ]
Ask a Meaningful Question
Getting someone to think and try something out is much more useful than just telling them the answer.
Joined: Sep 22, 2004
Nov 21, 2004 23:38:00
# 2) 5 in binary is 0101. Converted it give: 1010 which is: -8 + 0 + 2 + 0 = -6
waiting for the explaination of #1.
Step one step back to learn more thing..!!<br />Post to learn<br />learn from mistake<br />take whatever i understand...!!!<br /> <br />SCJP 1.4, Brainbench Java 1 & 2.<br />SCWCD 1.4 (preparing...!!)
I agree. Here's the link:
subject: Understanding Shift Operators
The shift >> doubt .......................
i don't get shift operators!!!!
Question on Shift Operators
Different between signs(Positive and Negative ) and values
Shift operator doubt
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2013