Created
November 11, 2014 22:35
-
-
Save farleylai/7a82d3a6f243ef43e179 to your computer and use it in GitHub Desktop.
This file contains 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 AbsTest { | |
public static int absSpec(int x) { | |
return x < 0 ? -x : x; | |
} | |
public static int absImpl(int x) { | |
int signbits = x >> 31; | |
int xor = x ^ signbits; | |
return xor + (signbits & 0x01); | |
} | |
public static void main(String []args) { | |
for(int i = -3; i < 5; i++) | |
System.out.printf("abs(%d): %s\n", | |
i, absImpl(i) == absSpec(i) ? "passed" : "failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment