Bitwise NOT behavior

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);
}

Surely an int is 32 bits on the Uno R4?

So int A prints as 16 bit but it's 32 bit too??

Serial.print() does not print leading zeroes

For B it prints 11111111111111111111111110011000. The last 8 bits are the correct value of -104 decimal. But the rest of the ones??

The leading zeros inverted.

They are the inverse of the zeroes that were in A before they were inverted by the bitwise NOT operator

Great, thanks a lot for the answers.

I am glad that I could help

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()
{
}

PrintBin.h

#include <Arduino.h>

template <typename varType>
void printBinBase(varType var, boolean splitBytes = false, boolean newLine = false )
{
  for (int bit = (sizeof(var) * 8) - 1; bit >= 0; bit--)
  {
    Serial.print( var >> bit & 1);
    if (splitBytes)
    {
      if (!(bit & 7))
      {
        Serial.print(" ");
      }
    }
  }
  if (newLine)
  {
    Serial.println();
  }
}

template <typename varType>
void printBin(varType var)
{
  printBinBase(var, false, false);
}

template <typename varType>
void printBinln(varType var)
{
  printBinBase(var, false, true);
}

template <typename varType>
void printBinBytes(varType var)
{
  printBinBase(var, true, false);
}

template <typename varType>
void printBinBytesln(varType var)
{
  printBinBase(var, true, true);
}