Skip to content

Instantly share code, notes, and snippets.

@vietj
Created February 4, 2019 22:02
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 vietj/93e23b2666e8bffc56d6ff100f171035 to your computer and use it in GitHub Desktop.
Save vietj/93e23b2666e8bffc56d6ff100f171035 to your computer and use it in GitHub Desktop.
private static int branchless1(byte c) {
// 48 == 11 0000
// 49 == 11 0001
// 50 == 11 0010
// 57 == 11 1001
byte b1 = (byte) ((c & 0b0001_0000) >> 4);
byte b2 = (byte) ((c & 0b0010_0000) >> 5);
byte b3 = (byte) ((c & 0b0100_0000) >> 6);
byte b4 = (byte) ((c & 0b1000_0000) >> 7);
byte b5 = (byte) (b1 & b2 & ~b3 & ~b4);
byte b6 = (byte) (b5 * 0xF);
byte b7 = (byte) (c & b6);
// '0' == 48 == 11 0000
// 0001
// 0010
// 0011
// ....
// 0111
// 1000
// 1001
return b7;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment