Skip to content

Commit 1b22595

Browse files
committed
feat: Add Bitwise6 class demonstrating signed right shift on negative number
Add a program that: - Initializes a negative integer using a binary literal - Applies a signed right shift (`>>`) by 1 bit - Outputs the result to show how sign extension works in Java - Prepares for unsigned right shift (`>>>`) with commented code for comparison.
1 parent d4f30ca commit 1b22595

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
public class Bitwise6
2-
{
1+
public class Bitwise6 {
32
public static void main(String[] args) {
43
int x = -0b1010;
54
int y;
65

7-
y=x>>>1;
8-
//y=x>>1;
6+
//y=x>>>1;
7+
y=x>>1;
98

109
System.out.println(y);
1110
}
1211
}
12+
1313
/*
1414
x = -0b1010 (Binary Representation)
1515
@@ -20,6 +20,7 @@ public static void main(String[] args) {
2020
2121
>>> is the logical right shift operator.
2222
Unlike >> (arithmetic right shift), >>> does not preserve the sign bit.
23+
2324
It fills the leftmost bit with 0 (zero-fill), regardless of whether x is positive or negative.
2425
Effectively divides by 2 for positive numbers, but changes negative numbers significantly.
25-
*/
26+
*/

0 commit comments

Comments
 (0)