-1

Suppose I was trying to create a mask in C for bitwise operations, like

uint32_t Mask = 0x00000003; 

What does the x mean? I see a lot of numbers written with that 0x format and I don't understand why and have been unable to find an explanation that really makes sense to me, maybe I'm not searching the right thing.

John Doe
  • 121
  • 1
  • 3
  • 9
  • 3
    Search for hexadecimal notation. – 001 Nov 08 '18 at 17:16
  • Stackoverflow is not a human driven Search engine. Please consider doing at least a tiny bit of research before asking such basic questions. Googling for "Numbers with 0x" gives several examples. – Yastanub Nov 08 '18 at 17:20

2 Answers2

4

it means the the number is expressed in hex , base 16 instead of decimal, based 10. For your example it makes no difference

uint32_t Mask = 0x00000003;

since 3 is 3 in both bases

but

uint32_t Mask = 0x00000010;  // '16' in human talk

is quite different from

uint32_t Mask = 10; // '10' in human talk

Note that

uint32_t Mask = 00000010; 

is actually '8' in human talk because in c and c++ numeric literals starting with a zero are in octal (base 8)

You will see

uint32_t Mask = 0x00000003;

when expressing something where the value is a set of bit flags rather than a number (see the name 'mask') since hex maps nicely to a sequence of bits. You might see

uint32_t Mask1 = 0x00000003;
uint32_t Mask2 = 0x00000010;
uint32_t Mask3 = 0x000000C2;

The first one doesnt need 0x but it just looks cleaner

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 3
    `00000010` is *8* in human talk - leading 0s indicate octal notation. – John Bode Nov 08 '18 at 18:00
  • 1
    @JohnBode - i just reread this answer (it got an upvote) and realized the error, scrolled down to edit and saw your comment - fixed now (if a little late) – pm100 Jun 10 '22 at 22:08
2

'0x' means that the number that follows is in hexadecimal. It's a way of unambiguously stating that a number is in hex, and is a notation recognized by C compilers and some assemblers.

Linny
  • 818
  • 1
  • 9
  • 22