Skip to content

Instantly share code, notes, and snippets.

@aybabtme
Created May 13, 2013 07:01
Show Gist options
  • Save aybabtme/5566602 to your computer and use it in GitHub Desktop.
Save aybabtme/5566602 to your computer and use it in GitHub Desktop.
public class Test {
public static void main(String[] args) {
byte first = 0x0F;
byte second = 0x0B;
byte concat = 0x00;
System.out.printf("Before, first=%s, second=%s, concat=%s.\n",
printBinary(first),
printBinary(second),
printBinary(concat));
concat = (byte) (((byte) (first << 4)) | second);
System.out.printf("After, first=%s, second=%s, concat=%s.\n",
printBinary(first),
printBinary(second),
printBinary(concat));
}
public static String printBinary(byte binaryNumber){
StringBuilder b = new StringBuilder(8);
int binary = 0 | binaryNumber;
int bitCount = 8;
for(int i = 0; i < bitCount; i++){
int j = 1 << bitCount - 1 - i;
if( (binary & j) != 0 ){
b.append('1');
} else {
b.append('0');
}
}
return b.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment