Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define FOO 1u 2u 4u ... What does 1u and 2u mean?

Tags:

c

typedef

I'm working with the HCS12 MCU, and this was part of the library. I'm just wondering what the 1U, 2U, 4U, 8U... means in this code. I'm still learning how to use classes, please try to explain things in layman terms.

My guess: Does this just mean which bit is set as high? 1U = 00000001 2U = 00000010 ...

What would this be called?

Thanks

    typedef union {
  byte Byte;
  struct {
    byte             :1; 
    byte ADR1        :1;                                       /* Slave Address Bit 1 */
    byte ADR2        :1;                                       /* Slave Address Bit 2 */
    byte ADR3        :1;                                       /* Slave Address Bit 3 */
    byte ADR4        :1;                                       /* Slave Address Bit 4 */
    byte ADR5        :1;                                       /* Slave Address Bit 5 */
    byte ADR6        :1;                                       /* Slave Address Bit 6 */
    byte ADR7        :1;                                       /* Slave Address Bit 7 */
  } Bits;
  struct {
    byte         :1;
    byte grpADR_1 :7;
  } MergedBits;
} IBADSTR;
extern volatile IBADSTR _IBAD @(REG_BASE + 0x000000E0UL);
#define IBAD                            _IBAD.Byte
#define IBAD_ADR1                       _IBAD.Bits.ADR1
#define IBAD_ADR2                       _IBAD.Bits.ADR2
#define IBAD_ADR3                       _IBAD.Bits.ADR3
#define IBAD_ADR4                       _IBAD.Bits.ADR4
#define IBAD_ADR5                       _IBAD.Bits.ADR5
#define IBAD_ADR6                       _IBAD.Bits.ADR6
#define IBAD_ADR7                       _IBAD.Bits.ADR7
#define IBAD_ADR_1                      _IBAD.MergedBits.grpADR_1
#define IBAD_ADR                        IBAD_ADR_1

#define IBAD_ADR1_MASK                  2U
#define IBAD_ADR2_MASK                  4U
#define IBAD_ADR3_MASK                  8U
#define IBAD_ADR4_MASK                  16U
#define IBAD_ADR5_MASK                  32U
#define IBAD_ADR6_MASK                  64U
#define IBAD_ADR7_MASK                  128U
#define IBAD_ADR_1_MASK                 254U
#define IBAD_ADR_1_BITNUM               1U
like image 840
user2066639 Avatar asked Feb 12 '13 23:02

user2066639


2 Answers

It defines an unsigned integer literal. You can also see where they defined a hex literal to be an unsigned long integer by using 0x...UL.

If you would like to know the bit pattern they produce, simply translate the decimal literals to their equivalent hex or binary literals. 1U becomes 0x01U and 01b1 in hex and binary respectively.

Another more commonly seen literal uses the f-suffix, that is a single precision floating point literal like 1.0f.

1. for illustration only, not an actual literal per the standard

like image 97
user7116 Avatar answered Oct 22 '22 15:10

user7116


They're just integer constants. The U suffix makes them unsigned ints instead of ints.

This is described in section 6.4.4.1 of the final draft of the C11 standard.

like image 3
Samuel Edwin Ward Avatar answered Oct 22 '22 14:10

Samuel Edwin Ward