In C++, you can represent numbers in different numeral systems.
#include <bitset>
std::bitset<8> bin1{ 0b1100'0101 }; // binary literal for binary 1100 0101
int x{ 012 }; // 0 before the number means this is octal
Decimal is the default numeral system.
int x{ 0xF }; // 0x before the number means this is hexadecimal
Decimal | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
---|---|---|---|---|---|---|---|---|
Hexadecimal | A | C | D | E | F | 10 | 11 |
int bin = 0b11; // binary 0000 0000 0000 0011
bin = 0b1; // same as binary 1
int hex = 0xF770; // binary 1111 0111 0111 0000
int bin2 = 0b11110000; // binary 0000 0000 1111 0000
long value { 2'132'673'462 }; // much easier to read than 2132673462
#include <format> // C++20
#include <print> // C++23
std::cout << std::format("{:b}\n", 0b1010); // C++20, {:b} formats the argument as binary digits
std::cout << std::format("{:#b}\n", 0b1010); // C++20, {:#b} formats the argument as 0b-prefixed binary digits
std::println("{:b} {:#b}", 0b1010, 0b1010); // C++23, format/print two arguments (same as above) and a newline
Scientific notation is a way to represent numbers that are too large or too small to be conveniently written in decimal form.
600.410 → 6.00410e2
0.0078900 → 7.8900e-3
Start with: 42030 (assuming the trailing zero is significant)
Slide decimal left 4 spaces: 4.2030e4
No leading zeros to trim: 4.2030e4
Keep trailing zeros: 4.2030e4 (5 significant digits)
Start with: 0.0078900
Slide decimal right 3 spaces: 0007.8900e-3
Trim leading zeros: 7.8900e-3
Don't trim trailing zeros: 7.8900e-3 (5 significant digits)
Escape sequences are used to represent special characters in a string.
Name | Escape Sequence | Description |
---|---|---|
Alert | \a | Makes an alert (beep) |
Backspace | \b | Moves cursor back one space |
Formfeed | \f | Moves cursor to next logical page |
Newline | \n | Moves cursor to next line |
Carriage return | \r | Moves cursor to beginning of line |
Horizontal tab | \t | Prints a horizontal tab |
Vertical tab | \v | Prints a vertical tab |
Single quote | \' | Prints a single quote |
Double quote | \" | Prints a double quote |
Backslash | \\ | Prints a backslash |
Question mark | \? | Prints a question mark |
Octal number | \number | Translates into char represented by octal |
Hex number | \xnumber | Translates into char represented by hex number |
std::cout << "Hello, World!\n"; // Prints "Hello, World!" followed by a newline
std::cout << "This is a tab:\tSee?\n"; // Prints a tab between "This is a tab:" and "See?"