Created
October 14, 2012 05:57
-
-
Save md-5/3887539 to your computer and use it in GitHub Desktop.
BitPacking.java
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 App | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
for (byte art = 0; art < 24; art++) | |
{ | |
for (byte data = 0; data < 4; data++) | |
{ | |
byte packed = pack(art, data); | |
byte uArt = getArt(packed); | |
byte uData = getFacing(packed); | |
System.out.println(packed); | |
if (art != uArt || data != uData) | |
{ | |
throw new Exception("Inputted Art: " + art + " Data: " + data + " Got Art: " + uArt + " Data: " + uData); | |
} | |
} | |
} | |
} | |
private static byte pack(byte art, byte facing) | |
{ | |
if (facing >= 4) | |
{ | |
throw new IllegalArgumentException("Facing must be between 0-3 inclusive"); | |
} | |
if (art >= 32) | |
{ | |
throw new IllegalArgumentException("Art must be between 0-31 inclusive"); | |
} | |
return (byte) (facing << 5 | art); | |
} | |
private static byte getFacing(byte in) | |
{ | |
return (byte) (in >> 5); | |
} | |
private static byte getArt(byte in) | |
{ | |
return (byte) (in & ~0x60); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment