aspose file tools*
The moose likes Beginning Java and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shift operators" Watch "Shift operators" New topic
Author

Shift operators

Anuradha Yerra
Greenhorn

Joined: Feb 09, 2002
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
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I am moving this thread to the Java beginner forum where it belongs. Thanks. See you there with a reply...


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    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

Joined: Feb 20, 2000
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
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    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

Joined: Jan 12, 2000
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


Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
David Brochstein
Greenhorn

Joined: Feb 20, 2000
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
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    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

Joined: Jan 12, 2000
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

Joined: Feb 20, 2000
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

Joined: Feb 16, 2000
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift operators