Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Bit Shifting

Ranch Hand
Posts: 34
• • • • Look at code snippet:

byte b=-42; //11010110
int result=b>>4;
=1111 1111 1111 1111 1111 1111 1101 0110 >>4
=1111 1111 1111 1111 1111 1111 1111 1101
=0xfffffffa //OK till here
result=-3 // (1) But how does it get this value I know since the resulting value is greater that what an int can hold but what is the formula
byte c= (byte)b>>4 // (2) Error, but i have done a legal cast
byte c=(byte) (b>>4)// (3) Ok, value still remains -3, why?
long l= (long)(b>>4)// (4) ok value still remains -3, why?
Since the resulting value of bit shifting is greater than what int var can hold , I put it in a long var ,but still value remains -3
And where can I get more questions related on this.
Pls.help
Thanks
Bindesh Vijayan

Ranch Hand
Posts: 110
• • • • If you want to know why the answer is -3, is to first see, how you can construct -3 from 3.
the binary rep for 3 is 0011. when you form -3 you form 2's complement. that is first you invert all the bits, 0's to 1's and 1's to 0's therefore you get 1100. and then you add 1 and the result is 1101 which is the representation of -3 that you have. so it's just the matter of working things other way round.
the second one marked with (2) you are doing a legal cast, but you are only casting the value b and not the result of b>>4 ! the lines marked with 3 and 4 can fit into byte and long because the range is ok. but the -3 comes from the explanation I described earlier.
HTH
[ July 14, 2002: Message edited by: Amir Ghahrai ]

Ranch Hand
Posts: 133
• • • • Hi Bindesh,

int result = b >> 4;
=1111 1111 1111 1111 1111 1111 1101 0110 >> 4
=1111 1111 1111 1111 1111 1111 1111 1101
=0xfffffffa //OK till here
result=-3 // (1) But how does it get this value I know since the resulting value is greater that what an int can hold but what is the formula

The resulting value is not greater than what an int can hold, since it's 32 bits, or the size of an int. The most significant bit is the sign bit, and since you did a signed shift, '1's were added to the left of the result of the shift. In 2's complement, 0xfffffffa == -3.

byte c= (byte)b>>4 // (2) Error, but i have done a legal cast

Not quite. What you've done here is just cast the b variable (which doesn't do anything, since b is a byte anyway), not the result of the shift operation.

byte c=(byte) (b>>4)// (3) Ok, value still remains -3, why?

This cast does what you intended for it to do.

long l= (long)(b>>4)// (4) ok value still remains -3, why?
Since the resulting value of bit shifting is greater than what int var can hold , I put it in a long var ,but still value remains -3

The result of putting the result of the shifting operation in a long is to just add a lot of '1's to the left of your result.
int - 0xfffffffa
long- 0xfffffffffffffffa
Hope this helps,
Paul Villangca

Bindesh Vijayan
Ranch Hand
Posts: 34
• • • • Thanks Amir& Paul,
But what about the rest part i.e.
the bits
1111 1111 1111 1111 1111 1111 1101 0110
will only 0110 be complemented in order to derive at -3 will the rest part be ignored.Bcox in hex representation it is oxfffffffa which means all the parts are considered.
And why will it be 2s complemented.
Iam all confused regards integer representation in Java. Pls.Help
Bindesh Vijayan
[ July 14, 2002: Message edited by: Bindesh Vijayan ]

Ranch Hand
Posts: 56
• • • • Some where on my hard drive I have a java program that takes a number, show its binary presentation, then do all those bit operators on them, and display the result in both the number it comes out to be and the binary presentation of the number.
What is the point of telling you this? There is a method some where that lets you print out binary presentation(thought it cuts off all the leading zeros). I'll suggest that you write a program that do what I had done. It helps me out a lot, since I get to see what I have to start with and what I end up with.
I'll dig around for that method and let you know what it is...I think its part of the wrapper class for all those number types.
Nope, I lied. I look it up, in Interger (the wrapper class) there are three methods that takes an int type and return a string representation of that int as binary, hex, or octal.

hope this helps
[ July 14, 2002: Message edited by: Chung Huang ]

Chung Huang
Ranch Hand
Posts: 56
• • • • -3 in 2's complement is
1111 1111 1111 1111 1111 1111 1111 1101
If you get the above binary number and wonder what is it in decimal, assume you know it is in 2's complement. Look at the left most bit, if it is one, then you are looking at a negative number, if its zero, then you are looking at a positive number. Why? Because that is what 2's complement means. In 2's complement, the left most bit serves the double purpose of representing a number and a flag for the sign of the number. This is why the maximum of a int is 2 to the power of 31 minus 1, because the left most bit being one would indicate a negative number. Back to translation. So how do you translate that long binary to -3? The easiest way I know of is this.
1. Count from the right most bit towards left and stop when you get to the first bit that is 1, we'll call this the index bit.
2. Invert all the bits that are left of the index bit.
3. Translate the resulting binary into decimal as usual.

Does this help you on translating a 2's complement negative number into decimal? Don't let hex or oct confuse you, deep down inside the computer, only 1 and 0 exist and so hex and oct don't mean a thing.
I have a file that is long, but it may help you. It take an integer and put that as the power of 2 and shows you what that would be in decimal and in binary. It deals with integer only, and it basically moves the 1 bit up and down and shows you what it will be in terms of decimal. If you want, I'll post the code for you.
[ July 14, 2002: Message edited by: Chung Huang ]

Ranch Hand
Posts: 375
• • • • Hi Bindesh,
Here are two points that might help you understand the problem .
1. JLS - 5.6.2 Binary Numeric Promotion
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.

2. JLS - 5.1.2 Widening Primitive Conversion
.....Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double in strictfp expressions also preserve the numeric value exactly
Ashish H.

Bindesh Vijayan
Ranch Hand
Posts: 34
• • • • Thanks Everybody
I got it all.  I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls. Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature