bitops.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import java.math.BigInteger;
  2. import java.nio.*;
  3. public class bitops {
  4. public static void main(String arg[]) {
  5. //ByteBuffer bb = ByteBuffer.allocate(4);
  6. //bb.putInt(200);
  7. //byte[] a3 = bb.array();
  8. //System.out.println(0b10000 + 0b11001000);
  9. //for(int i=0; i< a3.length ; i++) {
  10. //System.out.print(a3[i] +" ");
  11. //}
  12. /* a) - d) -> use BigInteger */
  13. BigInteger a1 = new BigInteger(Integer.toBinaryString(200), 2);
  14. BigInteger a2 = new BigInteger(Integer.toBinaryString(16), 2);
  15. System.out.println("16 + 200 = " + a1.add(a2).toString(2));
  16. System.out.println("16 - 200 = " + a1.subtract(a2).toString(2));
  17. BigInteger c1 = new BigInteger(Integer.toBinaryString(130), 2);
  18. int c2 = 3;
  19. System.out.println("130 >> 3 = " + c1.shiftRight(c2).toString(2));
  20. BigInteger d1 = new BigInteger(Integer.toBinaryString(130), 2);
  21. int d2 = 7;
  22. System.out.println("130 << 7 = " + d1.shiftLeft(d2).toString(2));
  23. /* e) - m) -> use int */
  24. System.out.println("250 >>> 3 = " + Integer.toString(250 >>> 3, 2));
  25. /* <<< doesn't work with int ??? */
  26. System.out.println("-3000 <<< 4 = " + Integer.toString(-3000 << 4, 2));
  27. System.out.println("~450" + Integer.toString(~450, 2));
  28. System.out.println("80 & 190" + Integer.toString(80 & 190, 2));
  29. System.out.println("100|(-390)" + Integer.toString(100 | (-390), 2));
  30. }
  31. }