Skip to content

Commit d4f30ca

Browse files
committed
feat: Add Bitwise5 class demonstrating right shift operation
Add a program that: - Initializes an integer using a binary literal - Applies a right shift (`>>`) by 1 bit - Prints the result, showing how bits move right and value is halved - Demonstrates the use of right shift in Java's bitwise operations
1 parent 446ce79 commit d4f30ca

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
public class Bitwise5
2-
{
1+
public class Bitwise5 {
32
public static void main(String[] args) {
4-
int x = 0b1000;
5-
3+
int x = 0b1000; //x = 8.
64
int y;
75

8-
y=x>>1;
6+
y=x>>1; //right shift by one decimal, or we can say that bits.
97

108
System.out.println(y);
119
}
1210
}
11+
1312
/*
1413
It shifts the bits of x one position to the right, while preserving the sign bit (leftmost bit).
1514
1615
Key Points:
1716
For positive numbers, it's equivalent to dividing by 2.
1817
For negative numbers, it rounds down (towards negative infinity).
1918
The leftmost bit (sign bit) remains the same, ensuring sign extension.
20-
*/
19+
*/

0 commit comments

Comments
 (0)