How to shift bits right in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {
 
        int n = 15;

        System.out.println(Integer.toBinaryString(n));

        n = n >> 1;
        
        System.out.println(Integer.toBinaryString(n));
    }
}

// 15 =      00001111
// 15 >> 1 = 00000111

/*
   
run:
   
1111
111
   
*/

 



answered Nov 2, 2016 by avibootz
0 votes
package javaapplication1;
 
public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        System.out.println(Integer.toBinaryString(2));
         
        System.out.println(Integer.toBinaryString(2 >> 1)); 
    }
}
 
// 2 =      0010
// 2 >> 1 = 0001
 
/*
    
run:
    
10
1
    
*/

 



answered Nov 2, 2016 by avibootz

Related questions

1 answer 123 views
123 views asked Feb 14, 2017 by avibootz
...