Skip to content

Instantly share code, notes, and snippets.

@farleylai
Created November 11, 2014 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farleylai/7a82d3a6f243ef43e179 to your computer and use it in GitHub Desktop.
Save farleylai/7a82d3a6f243ef43e179 to your computer and use it in GitHub Desktop.
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