Bit shifting fails with warning: left shift count >= width of type Given the following minimal working example. I tested this initially with uint16_t and it worked with no warnings. After that, I adjusted everything to work with uint32_t.

#include &lt;stdint.h&gt; /* needed for the uint16_t type*/
#include &lt;inttypes.h&gt; /* needed for sscanf uint16_t format placeholder  */
#include "printf.h"

#define SET_CMD_ID(ID)    ( (ID) &lt;&lt; 27)
#define SET_CMD_CMD(CMD)  ( (CMD)  &lt;&lt; 25)
#define SET_CMD_TYPE(TYPE)  ( (TYPE) &lt;&lt; 19)
#define SET_CMD_PKT(PKT)  ( (PKT)    )


void setup() {
  Serial.begin(115200);
  printf_begin();
  Serial.print(F("\n\rExample command\n\r: "));


}

void loop() {
  uint32_t payload = 0;

  uint8_t id = 3;   // 00000 - 5
  uint8_t cmd = 2;  // 00    - 2
  uint8_t type = 1; // 000000 - 6 
  uint32_t pkt = 0; // 0000000000000000000 - 19

  payload =  
        SET_CMD_ID(id)   |
        SET_CMD_CMD(cmd)  |
        SET_CMD_TYPE(type) |
        SET_CMD_PKT(pkt);
  Serial.print(F("Now sending "));
  Serial.println(payload);
  delay(2000);        // Try again soon
}


Verifying this sketch outputs:

/path/to/ino/Arduino32bitTest.ino: In function 'void loop()':
/path/to/ino/Arduino32bitTest.ino:5:37: warning: left shift count &gt;= width of type [enabled by default]
 #define SET_CMD_ID(ID)    ( (ID) &lt;&lt; 27)
                                     ^
/path/to/ino/Arduino32bitTest.ino:28:9: note: in expansion of macro 'SET_CMD_ID'
         SET_CMD_ID(id)   |
         ^
/path/to/ino/Arduino32bitTest.ino:6:39: warning: left shift count &gt;= width of type [enabled by default]
 #define SET_CMD_CMD(CMD)  ( (CMD)  &lt;&lt; 25)
                                       ^
/path/to/ino/Arduino32bitTest.ino:29:9: note: in expansion of macro 'SET_CMD_CMD'
         SET_CMD_CMD(cmd)  |
         ^
/path/to/ino/Arduino32bitTest.ino:7:41: warning: left shift count &gt;= width of type [enabled by default]
 #define SET_CMD_TYPE(TYPE)  ( (TYPE) &lt;&lt; 19)
                                         ^
/path/to/ino/Arduino32bitTest.ino:30:9: note: in expansion of macro 'SET_CMD_TYPE'
         SET_CMD_TYPE(type) |
         ^

Sketch uses 3,174 bytes (9%) of program storage space. Maximum is 32,256 bytes.
Global variables use 216 bytes (10%) of dynamic memory, leaving 1,832 bytes for local variables. Maximum is 2,048 bytes.


Executing the sketch on an Arduino Uno results in:

Example command

: Now sending 0
Now sending 0
Now sending 0
Now sending 0


I googled the message of the warning and found out that it could mean that I'm trying to shift 32bit which is undefined, but I'm not sure on that. So my question is:
What am I doing wrong here? How can I get this to work?
It's basically an inability of the compiler to auto-promote the source variables to a size big enough to fit the shifted version.

The default type for all operations, unless otherwise specified, is 16-bit. By shifting more than 16 bits you are overflowing that default type.

To get around it you have to manually tell the compiler that you want to work with 32-bits:

#define SET_CMD_ID(ID)      ( ((uint32_t)ID) &lt;&lt; 27)
#define SET_CMD_CMD(CMD)    ( ((uint32_t)CMD)  &lt;&lt; 25)
#define SET_CMD_TYPE(TYPE)  ( ((uint32_t)TYPE) &lt;&lt; 19)
#define SET_CMD_PKT(PKT)    ( ((uint32_t)PKT)    )