Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Demo {
- public static void main (String args[]) {
- byte d = 127; // 8-bit is equal to 0 111 1111
- int e = d << 2; /* 32-bit shifted value is equal to 0 000 0000 0000 0000 0000 0001 1111 1100
- 0 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3 + 1 * 2^4 + 1 * 2^5 + 1 * 2^6 + 1 * 2^7 + 1 * 2^8 =
- 0 * 1 + 0 * 2 + 1 * 4 + 1 * 8 + 1 * 16 + 1 * 32 + 1 * 64 + 1 * 128 + 1 * 256 =
- 0 + 0 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 508
- */
- byte f = (byte)(e); /* [] indicates 8-bit at 32-bit shifted value: 0 000 0000 0000 0000 0000 0001 [1111 1100]
- 0 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3 + 1 * 2^4 + 1 * 2^5 + 1 * 2^6 - 1 * 2^7 =
- 0 * 1 + 0 * 2 + 1 * 4 + 1 * 8 + 1 * 16 + 1 * 32 + 1 * 64 - 1 * 128 =
- 0 + 0 + 4 + 8 + 16 + 32 + 64 - 128 = -4
- */
- int g = (byte)(e);
- }
- }
- byte a = 10;
- System.out.println(Integer.toBinaryString(a));
- int b = 10;
- System.out.println(Integer.toBinaryString(b));
- byte c = 5 << 1;
- System.out.println(Integer.toBinaryString(c));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement