I bitwise NOT variable A below. int A is 16 bit. However, int B = ~ A prints B as 1111 1111 1111 1111 1111 1111 1001 1000, but B decimal is correctly -104. Am I doing something incorrectly when printing the binary?? Why is B printed as 32 bit?? Thanks for any hints. AA
void setup() {
Serial.begin (9600);
int A = 103; // binary: 0000000001100111
int B = ~A; // binary: 1111111110011000 = -104
Serial.println ();
Serial.print ("Bitwise NOT ~. In setup A: ");
Serial.println (A, BIN);
Serial.println (A, DEC);
Serial.print ("In setup B = ~A: ");
Serial.println (B, BIN);
Serial.println (B, DEC);
}
I do think that it is a shame that printing of leading zeroes is suppressed
You might like to try this. The variable can be any type of integer
#include "PrintBin.h"
/*
printBin(var); //print bits
printBinln(var); //print bits followed by newline
printBinBytes(var); //print bits with spaces between bytes to improve readbility
printBinBytesln(var); //print bits with spaces between bytes to improve readbility followed ny newline
*/
void setup()
{
Serial.begin(115200);
while (!Serial);
int var = 1;
printBin(var);
Serial.println("\nnewline");
printBinln(var);
Serial.println("\nnewline");
printBinBytes(var);
Serial.println("\nnewline");
printBinBytesln(var);
Serial.println("\nnewline");
}
void loop()
{
}