Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Demo {
  2. public static void main (String args[]) {
  3.  
  4. byte d = 127; // 8-bit is equal to 0 111 1111
  5.  
  6. int e = d << 2; /* 32-bit shifted value is equal to 0 000 0000 0000 0000 0000 0001 1111 1100
  7. 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 =
  8. 0 * 1 + 0 * 2 + 1 * 4 + 1 * 8 + 1 * 16 + 1 * 32 + 1 * 64 + 1 * 128 + 1 * 256 =
  9. 0 + 0 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 508
  10. */
  11.  
  12. byte f = (byte)(e); /* [] indicates 8-bit at 32-bit shifted value: 0 000 0000 0000 0000 0000 0001 [1111 1100]
  13. 0 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3 + 1 * 2^4 + 1 * 2^5 + 1 * 2^6 - 1 * 2^7 =
  14. 0 * 1 + 0 * 2 + 1 * 4 + 1 * 8 + 1 * 16 + 1 * 32 + 1 * 64 - 1 * 128 =
  15. 0 + 0 + 4 + 8 + 16 + 32 + 64 - 128 = -4
  16. */
  17.  
  18. int g = (byte)(e);
  19.  
  20. }
  21. }
  22.  
  23. byte a = 10;
  24. System.out.println(Integer.toBinaryString(a));
  25. int b = 10;
  26. System.out.println(Integer.toBinaryString(b));
  27.  
  28. byte c = 5 << 1;
  29. System.out.println(Integer.toBinaryString(c));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement