I have written a function to print the bit out in a byte. This happens by setting the most significant bit to 1 comparing it to the inputbyte and if it is also 1 printing "1" else "0". The comparison byte is then shifted right.
How do I achieve starting with 10000000 and shifting to 01000000 then 00100000...
I believe my problem is caused by promotion to int then recasting but I don't see the solution.
package errorreporting;
public class ErrorReporting {
ErrorReporting() {
}
public static void main(String[] args) {
ErrorReporting myError = new ErrorReporting();
byte myByte = 16;
myError.printByteArray(myByte);
}
public void printByteArray(byte inputByte) {
// print out 1 or 0
byte comparison = -128;
for (int x = 0; x < 8; x++) {
if ((inputByte & comparison) != 0) {
System.out.print("1");
} else {
System.out.print("0");
}
// System.out.print(" comparison : " + comparison);
comparison = (byte) (comparison >>> 1);
}
System.out.println(" : " + inputByte);
}
}
This post has some info : Java bitshift strangeness