Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitwise LeftShift and RightShift

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
I am learning Bitwise Shift operators, and my question is (Pl see below commented lines in the Program)

Thanks

public class Bitwise {
int i =1;
int j = 1;
public static void main(String args[]) {
Bitwise obj = new Bitwise();
Bitwise obj1 = new Bitwise();
System.out.println(obj.i <<= 31);
//Here I am expecting a value of 2 power 31 but i am getting a negative value
System.out.println(obj.i >>= 31);
//Here I am expecting a value of 1, but I am getting value = -1.
System.out.println(obj.i >>= 1);
//here i am expecting value = 0.5 or 0 since it is integer type but i am getting value of -1.

System.out.println(obj1.j >> 31);
//Here i am expecting a value of 2 power-31 value but i am getting value 0
System.out.println(obj1.j >>= 31);
//Here i am expecting a value of 2 power -62 which is out of range for integer type,but iam getting value of 0.


}

}
 
Code:
public class bw {
   int i =1;
   int j = 1;
   int m =-1;
        public static void main(String args[]) {
        bw obj = new bw();
        bw obj1 = new bw();
        System.out.println(obj.i <<= 31);
        // 00000000 00000000 00000000 00000001
        // 00000000 00000000 00000000 00000010
        // 00000000 00000000 00000000 00000100
        // ...
        // 10000000 00000000 00000000 00000000
        //Here I am expecting a value of 2 power 31 but i am getting  a negative value 
        // because the leftest bit became 1. It is negative in 2's complement

        System.out.println(obj.i >>= 31);
        // 10000000 00000000 00000000 00000000 become
        // 11000000 00000000 00000000 00000000 become
        // 11100000 00000000 00000000 00000000 become
        // ...
        // 11111111 11111111 11111111 11111111 (this is negative one in 2's complement)

        // After each right shift, a new bit is added on the leftest bit. A new bit '1' is added to a negative number,
        // a new bit '0' is added to positive number, so right shift will keep negative number maintain negative after right shift
        //Here I am expecting a value of 1, but I am getting value = -1.

        //System.out.println(obj.m >>>= 31);
        //unsigned right shift, A new bit '0' is added to the leftest bit whether positive or negative number

        System.out.println(obj.i >>= 1);
        //here i am expecting  value = 0.5 or 0 since it is integer type but i am getting value of -1.
        
        System.out.println(obj1.j >> 31);
        // 00000000 00000000 00000000 00000001 become
        // 00000000 00000000 00000000 00000000 become
        //   ...
        // 00000000 00000000 00000000 00000000
        // after each right shift, a new bit '0' is added to the leftest bit for right shifting a postive number
        //Here i am expecting a value of 2 power-31 value but i am getting value 0
        System.out.println(obj1.j >>= 31);
        //Here i am expecting a value of 2 power -62 which is out of range for integer type,but iam getting value of 0.
      
      
    }
    
}
 
Here I am expecting a value of 2 power 31 but i am getting a negative value
Java only has signed numbers. It uses the high bit as the sign indicator, so when you set it, your number goes negative.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top