Search code examples
cbit-shift

Left Bitshift operation. Is this calculation correct?


What is the result of the following bitshift operation:

  ((((uint32)   0x0) << 6U) |  
   (((uint32)   0x2) << 4U) |  
   (((uint32)   0x0) << 2U) |  
   (((uint32)   0x1) << 0U))   

I would expect:

0|32|0|1 = 33 decimal

Is this right or I'm totally wrong?


Solution

  • You can run the code and see for yourself. Let me write a printf statement for you.

    #include <stdio.h>
    #include <stdint.h>
    
    int main (void)
    {
      printf("%u\n",   
        (((uint32_t)   0x0) << 6U) |  
        (((uint32_t)   0x2) << 4U) |  
        (((uint32_t)   0x0) << 2U) |  
        (((uint32_t)   0x1) << 0U) );
    }