uint16_t and uint32_t question!

Hi,
I am getting confused between uint16 and uint32.
uint16 has to 2 bytes[16 bits] and uint32 has 4 bytes[32 bits].

In my problem, uint16t value= 1112 , when i convert 1112 into binary i get 10001011000 [only 12 bits are present].

So how does the structure of 16 bits of 1112 look?

Second question
uint16t value= 1112

How does (uint32_t)value look? [32 bits representation of 1112]

Any pointers would be helpful

Regards
Niran

It looks exactly as described. The upper bits are simply zero and you've omitted them. But they are indeed part of the variable.

Yeah

so 10001011000 and 0000010001011000 would be uint8 and uint16 bit representations of 1112.
And the upper bits would keep getting zeros as long as the size of the datatype extends further as uint32 and uint64 right for this value(1112) as an example?

Thank you

We need to review prevailing concepts:

1. We have the decimal number 1112.

2. We want to save the number of Step-1 into memory locations of the ATmega328P MCU of the Arduino UNO Board. One memory location of the MCU holds 8-bit data. How many memory locations do we need to store 1112? By hand calculation, we find that 1112 turns to be 10001011000 in bit form. There are 11-bit data; so need about 1.375 memory locations to save the 11-bit binary data. Because there is no way to accept fractional memory locations such as 1.375, we must accept two consecutive memory locations. The lower 8-bit (01011000) of the 11-bit data will be saved into the 1st memory location; the upper 3-bit (100) data will be transformed into 8-bit data after appending five 0s to its left (00000100) and then the resultant 8-bit number (00000100) will be saved in the next high order memory location.

3. Now, we need to make a request to the Compiler to allocate 16-bit (2-byte) memory space for us to store the number of Step-2. The request is made by the following declaration:

unsigned int x = 0b0000010001011000;

or

unsigned int x = 0x0458;  //hex representation of 1112

or

unsigned int x = 1112;    //the Compiler automatically saves 00000100 01011000 into memory 

or 

uint16_t x = 1112;

What is the difference between these two styles: 'unsigned int' and 'uint16_t'? Please, make a search at the net.

4. We may execute the following codes to see what has been exactly saved into memory locations in response to this declaration: unsigned int x = 1112;.

void setup()
{
  Serial.begin(9600);
  unsigned int x = 1112;
  for (int i = 15; i >= 0; i--) //shows: 00000100 01011000
  {
    bool n = bitRead(x, i);
    if (n == 0)
    {
      Serial.print('0');  //forcibly showing 0 as Serial Monitor does not show leading 0s
    }
    else
    {
      Serial.print(n, BIN);
    }
  }
}

void loop() 
{

}

Niranjan_ravi:
so 10001011000 and 0000010001011000 would be uint8 and uint16 bit representations of 1112.

Nope. 1112 does not fit into eight bits of storage. The most significant bits are truncated leaving...

  0b01011000

Niranjan_ravi:
so 10001011000 and 0000010001011000 would be uint8 and uint16 bit representations of 1112.

Is that a typo ?

How can 10001011000 (11 bits) fit in a uin8_t (8 bits) variable ?

The code to print a binary 16 bit value is much too complicated, and is implying non-existent behaviour.

There is no magic involved, just print the read bits.

  for (char i = 15; i >= 0; i--)  {
    Serial.print(bitRead(x, i));
  }

Why does this code: 'Serial.print(bitRead(x, i));' print leading 0s on the Serial Monitor and not this code: 'Serial.print(x, BIN);'?
sm5.png

sm5.png

Because bits don't have leading zeros.