forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightShiftOperator.java
47 lines (41 loc) · 2 KB
/
RightShiftOperator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.rampatra.bits;
/**
* {@code >>} shifts bits to right filling left bits with the left most
* bit (most significant bit). Also called signed right shift.
* {@code >>>} shifts bits to the right filling left bits
* with 0. Also called unsigned right shift.
*
* @author rampatra
* @since 6/2/15
*/
public class RightShiftOperator {
public static void main(String[] args) {
int n = -4;
System.out.printf("n: %32d\n", n);
System.out.printf("n: %32s\n", Integer.toBinaryString(n));
System.out.printf("n>>1: %32s\n", Integer.toBinaryString(n >> 1));
System.out.printf("n>>1: %32d\n", n >> 1);
System.out.printf("n>>>1: %32s\n", Integer.toBinaryString(n >>> 1));
System.out.printf("n>>>1: %32d\n", n >>> 1);
System.out.println("=======================================");
n = -235034334;
System.out.printf("n: %32d\n", n);
System.out.printf("n: %32s\n", Integer.toBinaryString(n));
System.out.printf("n>>1: %32s\n", Integer.toBinaryString(n >> 1));
System.out.printf("n>>1: %32d\n", n >> 1);
System.out.printf("n>>>1: %32s\n", Integer.toBinaryString(n >>> 1));
System.out.printf("n>>>1: %32d\n", n >>> 1);
System.out.printf("n>>2: %32s\n", Integer.toBinaryString(n >> 2));
System.out.printf("n>>>2: %32s\n", Integer.toBinaryString(n >>> 2));
System.out.println("=======================================");
n = 235034334;
System.out.printf("n: %32d\n", n);
System.out.printf("n: %32s\n", Integer.toBinaryString(n));
System.out.printf("n>>1: %32s\n", Integer.toBinaryString(n >> 1));
System.out.printf("n>>1: %32d\n", n >> 1);
System.out.printf("n>>>1: %32s\n", Integer.toBinaryString(n >>> 1));
System.out.printf("n>>>1: %32d\n", n >>> 1);
System.out.printf("n>>2: %32s\n", Integer.toBinaryString(n >> 2));
System.out.printf("n>>>2: %32s\n", Integer.toBinaryString(n >>> 2));
}
}