The use of hexadecimal or binary notation to define a number will automatically create an unsigned integer using a representation that is just large enough to hold the specified value. For example:
0b101 # uint8 0x100 # uint16 0xDEADBEEF # uint32 0x1DEADBEEF # uint64
The storage class can be specified by adding a suffix. Use ‘s’ for signed integers and ‘u’ for unsigned integers along with a size (‘8’, ‘16’, ‘32’, ‘64’). The use of the underscore separator ‘_’ can improve readability. For example:
0b101s16 # int16 0b101_s16 # int16, value and representation separated 0xDEADBEEFs32 # int32 0xDEADBEEF_u64 # uint64
Note that when defining matrices of integer constants the overall matrix will
have the storage class of its first element. The matrix
[0x1; 0x100; 0x10000]
will be of type uint8
and the larger values
will be truncated because of the saturation semantics of integer values. To
avoid this issue either: 1) declare the first integer to be of the desired size
such as 0x1u32; 0x100; 0x10000]
, or 2) pad constants in array
expressions with leading zeros so that they use the same number of digits for
each value such as [0x00_00_01; 0x00_01_00; 0x01_00_00]
.