So, I'm working with Java, and testing out various bitwise functions, shifts, whatnot. When I do whatever bitwise operations, I want to be able to see things on a bit level - that is, after doing:
Code:
Instead of having it display 01100110 like I would want it to, it displays 102. Is there a routine somewhere that displays a decimal number as a binary one?
EDIT: Welp, this is embarrassing. At the time of writing I had written a routine that does what I want, but it had some errors; so I posted about it, and fixed the errors less than five seconds after making the post. Anyways, here's the routine.
Code:
The routine accepts a byte as a parameter, and returns a string version of the binary digits. If someone has a much better/more optimized way of doing this, please let me know!
Code:
byte a = (byte) 0b10011001;
a = ~a;
System.out.print( a );
Instead of having it display 01100110 like I would want it to, it displays 102. Is there a routine somewhere that displays a decimal number as a binary one?
EDIT: Welp, this is embarrassing. At the time of writing I had written a routine that does what I want, but it had some errors; so I posted about it, and fixed the errors less than five seconds after making the post. Anyways, here's the routine.
Code:
public static String Binarify( byte ByteToCheck ) {
String binaryCode = "";
byte[] reference = new byte[]{ (byte) 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
for ( byte z = 0; z < 8; z++ ) {
//if bit z of byte a is set, append a 1 to binaryCode. Otherwise, append a 0 to binaryCode
if ( ( reference[z] & ByteToCheck ) != 0 ) {
binaryCode += "1";
}
else {
binaryCode += "0";
}
}
return binaryCode;
}
The routine accepts a byte as a parameter, and returns a string version of the binary digits. If someone has a much better/more optimized way of doing this, please let me know!