1

I'm learning C++ through a website - learncpp.com and I encountered a line of code that confuses me and I'm not sure how to research it. I went back looking for an explanation on what it is and couldn't find it and I would simply like to know what to look up or what it's called so I could learn more about it. Here's the code:

    unsigned char option_viewed = 0x01;

It's referring to a bit flag. What i'm confused about is the 0x01; part. I'm assuming it's hexadecimal for one, but I'm not sure. Thanks

rockorcus
  • 25
  • 4
  • 1
    Yes, `0x01` is a hexadecimal [integer literal.](https://en.cppreference.com/w/cpp/language/integer_literal). It's just another way to write `1`. – Igor Tandetnik Sep 09 '18 at 01:34
  • here is a detail thread for more clarification https://stackoverflow.com/questions/8186965/what-do-numbers-using-0x-notation-mean – Janaka Sep 09 '18 at 01:53

2 Answers2

2

When creating bit flags, people often write the literal values in hex because they are easier to read:

0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80

Instead of:

1, 2, 4, 8, 16, 32, 64, 128
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

In C++ you can represent an integer literal in 3 different bases: decimal (base 10), octal (base 8), or hexidecimal (base 16). Numbers written the "normal" way (1, 13, 405, ...) are interpreted by the compiler as base 10. Numbers starting with a 0 (01, 040, 0800, ...) are interpreted as base 8. Numbers starting with a 0x (0x1, 0x01, 0x800, ...) are interpreted as base 16.

Note that with base 16 numbers, you can have as many leading 0's after the 0x as you want. That is, 0x1, 0x01, and 0x00000001 are all just 1. The reason you might want to include more leading 0's is for alignment of the code to help with readability. As John Zwinck pointed out it's common for people to use hexidecimal to represent bit flags because every power of 2 can be written with either a 1, 2, 4, or 8 in one of the digits. So you might see something like

unsigned char red     = 0x01;  // 1 base 10,   00000001 base 2
unsigned char green   = 0x02;  // 2 base 10,   00000010 base 2
unsigned char blue    = 0x04;  // 4 base 10,   00000100 base 2
unsigned char yellow  = 0x08;  // 8 base 10,   00001000 base 2
unsigned char magenta = 0x10;  // 16 base 10,  00010000 base 2
unsigned char cyan    = 0x20;  // 32 base 10,  00100000 base 2
unsigned char white   = 0x40;  // 64 base 10,  01000000 base 2
unsigned char black   = 0x80;  // 128 base 10, 10000000 base 2

Really each variable stores a numeric value, but because we're using powers of 2 then each value is represented by a different bit being set to 1. Since the type is an unsigned char which is only 8-bits, you'll never encounter a power larger than 255, or 0xFF hexidecimal, so two digits after the 0x is enough. If you were storing an unsigned short then you might want to write it as 0x0001 which is still just 1, but you're making it obvious that it's a 16-bit integer rather than 8-bit.

You can read more about the different numerical bases in C++ here.

Moohasha
  • 245
  • 1
  • 13