|
| 1 | +<div align=right> |
| 2 | + |
| 3 | + 🌎 [中文] | [English] |
| 4 | +</div> |
| 5 | + |
| 6 | +[中文]: ../../cpp14/05-binary-literals.html |
| 7 | +[English]: ./05-binary-literals.html |
| 8 | + |
| 9 | +# Binary Literals |
| 10 | + |
| 11 | +C++14 introduces the `0b` / `0B` prefix for binary integer literals, making bit-level values directly readable |
| 12 | + |
| 13 | +| Book | Video | Code | X | |
| 14 | +| --- | --- | --- | --- | |
| 15 | +| [cppreference-integer_literal](https://en.cppreference.com/w/cpp/language/integer_literal) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/05-binary-literals.md) | [Video Explanation]() | [Exercise Code](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/05-binary-literals-0.cpp) | | |
| 16 | + |
| 17 | + |
| 18 | +**Why introduced?** |
| 19 | + |
| 20 | +Bit masks and flag bits expressed in decimal or hex cannot directly convey the binary bit layout. `0b0010'1100` makes every bit's meaning visible at a glance |
| 21 | + |
| 22 | +## I. Basic Usage and Scenarios |
| 23 | + |
| 24 | +```cpp |
| 25 | +int a = 0b1010; // 10 |
| 26 | +int b = 0B1111; // 15 |
| 27 | + |
| 28 | +// Bit masks — binary representation is the most intuitive |
| 29 | +constexpr unsigned READ = 0b001; |
| 30 | +constexpr unsigned WRITE = 0b010; |
| 31 | +constexpr unsigned EXEC = 0b100; |
| 32 | + |
| 33 | +unsigned perm = 0b101; // READ | EXEC |
| 34 | +``` |
| 35 | + |
| 36 | +## II. Real-World Case — Binary Literals in the STL |
| 37 | + |
| 38 | +> The MSVC STL uses binary literals for bit masks in Unicode handling. The example below cites the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/format`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/format#L259-L267)) |
| 39 | +
|
| 40 | +```cpp |
| 41 | +// MSVC STL · msvc-stl/stl/inc/format (abridged) |
| 42 | +// UTF-8 encoding — mask out the effective bits per byte count |
| 43 | +switch (_Num_bytes) { |
| 44 | +case 2: |
| 45 | + _Val &= 0b1'1111u; // 2 bytes: keep lower 5 bits |
| 46 | + break; |
| 47 | +case 3: |
| 48 | + _Val &= 0b1111u; // 3 bytes: keep lower 4 bits |
| 49 | + break; |
| 50 | +case 4: |
| 51 | + _Val &= 0b111u; // 4 bytes: keep lower 3 bits |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## III. Notes |
| 56 | + |
| 57 | +- Binary literals can only be used with integer types |
| 58 | +- Can be combined with digit separators: `0b1010'1100` |
| 59 | +- Both `0b` and `0B` work; `0b` is more common |
| 60 | + |
| 61 | +## IV. Exercise Code |
| 62 | + |
| 63 | +### Exercise Topics |
| 64 | + |
| 65 | +- 0 - [Binary Literals — Bit Mask Operations](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/05-binary-literals-0.cpp) |
| 66 | + |
| 67 | +### Auto-Checker Command |
| 68 | + |
| 69 | +``` |
| 70 | +d2x checker binary-literals |
| 71 | +``` |
| 72 | + |
| 73 | +## V. Other |
| 74 | + |
| 75 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 76 | +- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp) |
| 77 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 78 | +- [Tutorial Support Tool - xlings](https://github.com/openxlings/xlings) |
0 commit comments