Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shift operators

Greenhorn
Posts: 3
public class B{
public static void main(String args[]){
int n=-1; //(How do I calculate the binary for a negative number?)
n=n<<1;<br /> System.out.println(n); //(How is n -2 ?)<br /> int n1=-1;<br /> n1=n1>>1;
System.out.println(n1);//(How is n1 -1 ?)
int n2=-1;
n1=n2>>>1;
System.out.println(n2); //(How is n2 -1 ?)
}
}

Tony Alicea
Sheriff
Posts: 3226
5
I am moving this thread to the Java beginner forum where it belongs. Thanks. See you there with a reply...

Tony Alicea
Sheriff
Posts: 3226
5
"How do I calculate the binary for a negative number?"
The format is called two's complement notation.
First take the positive of the (signed) integral type (byte, short, int or long) and invert all bits. For example, lets take a byte equal to one:
00000001
invert all bits (also known as taking the ONE'S complement. The operator is ~)
11111110
add 1 and you get all ones:
11111111
That's -1 in binary.

David Brochstein
Greenhorn
Posts: 4
I compiled this, and the results interest me:
why does n change from -1 to -2, but n1 stays -1 after >> ?

Tony Alicea
Sheriff
Posts: 3226
5
Because the <CODE>>></CODE> drags the most significant bit with it to the right with the effect that all the positions that are vacated because of the shift towards the right, are filled with whatever the sign bit is.
On the other hand, the <CODE>>>></CODE> operator will fill the leftmost bits with ZERO always.
The first one is called a signed shift to the right and the other an unsigned shift.
The <CODE><<</CODE> operator will fill the RIGHT part of the number with ZERO always.

Ray Marsh
Ranch Hand
Posts: 458
Can I ask why anyone would want to do this? It seems confusing. If I want to do something to a numeric I normally use a math function i.e. ( + - * / ) In what type of situation would a shift operator be more appropriate?
Thanks, Ray

David Brochstein
Greenhorn
Posts: 4
Ray,
see my posting in topic: Bits next to topic: Shift operators. It's the same question, I think.. Feel free to add to it. No response yet...

Tony Alicea
Sheriff
Posts: 3226
5
It's a very efficient way to multiply and divide numbers by powers of two, for one thing...
Also, especially in assembly language, it was/is used to pack and unpack small pieces of data in a relatively small area of memory.
It has been around for a while... shifting.

Ray Marsh
Ranch Hand
Posts: 458
Tony,
I'm not trying to be difficult, however in the interest of readable and maintainable code, wouldn't multiplying by -1 be easier to read and understand than a shift operation? Or, is a shift operation to negate a numeric just as obvious to someone familiar with the function? Or, is it that much faster to execute?
We have bit op-codes in RPG, but they are rarely used. I myself have never used them. Is this common practice in Java?
Thanks

nirvan sage
Greenhorn
Posts: 24

0000 0001 is the eight bit representation of 1
(take care that number represntation differs in int and long)
in order to change the sign take the twos complement
which is
1111 1110
+1
---------
1111 1111
This is the binary representation if -1
shifting it by one time towards the left makes it
1111 1110
This is actually a negative representation of some number
Inorder to find the actual positive number we just take
the twos complement again.
0000 0001
+1
---------
0000 0010
which is 2 so
1111 1110 must be -2

For the second case the last 1 in the LSB is not destroyed.
As for the third question you are assigning a variable and printing
the same variable.

Theresa Duick
Greenhorn
Posts: 27
I am having a tough time understanding binarys also. When the binary begins with a 0 on the left side, is it considered a positive # regardless of how many 1's appear to it's right side? Vice versa, if the left most # is a 1, is the # then considered negative?