Created
January 23, 2016 17:03
-
-
Save kishida/95f1fb4d2aa48245b59e to your computer and use it in GitHub Desktop.
Add only with logical operation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Add { | |
public static void main(String[] args) { | |
int a = 0b01100101; | |
int b = 0b00110110; | |
System.out.printf("a:%d b:%d a+b:%d%n", a, b, a + b); | |
int and = a & b; | |
System.out.printf("%8s(%d)%n", Integer.toBinaryString(a), a); | |
System.out.printf("%8s(%d)%n", Integer.toBinaryString(b), b); | |
System.out.printf("%8s(%d) xor%n", Integer.toBinaryString(a ^ b), a ^ b); | |
System.out.printf("%8s(%d) and%n", Integer.toBinaryString(and), and); | |
System.out.println(); | |
a = a ^ b; | |
b = and << 1; | |
and = a & b; | |
System.out.printf("%8s(%d) and << 1%n", Integer.toBinaryString(b), b); | |
System.out.printf("%8s(%d) xor%n", Integer.toBinaryString(a ^ b), a ^ b); | |
System.out.printf("%8s(%d) and%n", Integer.toBinaryString(and), and); | |
System.out.println(); | |
a = a ^ b; | |
b = and << 1; | |
and = a & b; | |
System.out.printf("%8s(%d) and << 1%n", Integer.toBinaryString(b), b); | |
System.out.printf("%8s(%d) xor%n", Integer.toBinaryString(a ^ b), a ^ b); | |
System.out.printf("%8s(%d) and%n", Integer.toBinaryString(and), and); | |
System.out.println(); | |
} | |
} |
Author
kishida
commented
Jan 23, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment