Skip to content

Commit 4cb2ce5

Browse files
committed
feat(cpp14): add 05-binary-literals
Book chapter (zh + en): - section 一/I: Basic usage — binary flags and bit masks - section 二/II: Real-world case — UTF-8 encoding masks from msvc-stl/stl/inc/format#L259-L267 - section 三/III: Notes — integer only, digit separator combination - section 四/IV: Exercise topics and d2x checker command - section 五/V: External resources Exercise: 0. Bit mask operations with binary literals. 3 D2X_YOUR_ANSWER. Build wiring: - register 05 target in dslings/cpp14, dslings/en/cpp14, solutions/cpp14 - add 05 entry to zh/en SUMMARY.md
1 parent 1c37ddd commit 4cb2ce5

10 files changed

Lines changed: 306 additions & 0 deletions

File tree

book/en/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
- [Generic Lambdas](./cpp14/00-generic-lambdas.md)
3434

35+
- [Binary Literals](./cpp14/05-binary-literals.md)
36+
3537
# Additional Resources
3638

3739
- [Changelog](changelog.md)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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)

book/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
- [泛型 lambda - generic lambdas](./cpp14/00-generic-lambdas.md)
3434

35+
- [二进制字面量 - binary literals](./cpp14/05-binary-literals.md)
36+
3537
# 其他
3638

3739
- [更新日志](changelog.md)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<div align=right>
2+
3+
🌎 [中文] | [English]
4+
</div>
5+
6+
[中文]: ./05-binary-literals.html
7+
[English]: ../en/cpp14/05-binary-literals.html
8+
9+
# 二进制字面量 - binary literals
10+
11+
C++14 引入 `0b` / `0B` 前缀表示二进制字面量, 让位运算和底层编程中的数值表示更加直观
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/src/cpp14/05-binary-literals.md) | [视频解读]() | [练习代码](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/cpp14/05-binary-literals-0.cpp) | |
16+
17+
18+
**为什么引入?**
19+
20+
位掩码和标志位用十进制或十六进制表示时, 无法直接对应二进制位布局。`0b0010'1100` 让每一位的含义肉眼可见
21+
22+
## 一、基础用法和场景
23+
24+
```cpp
25+
int a = 0b1010; // 10
26+
int b = 0B1111; // 15
27+
28+
// 位掩码 — 二进制表示最直观
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+
## 二、真实案例 - STL 中的二进制字面量
37+
38+
> MSVC STL 在 Unicode 处理中使用二进制字面量表示位掩码, 配合数字分隔符更加清晰。下面以仓库内置的 [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) 为例 (源码: [`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 (有删节)
42+
// UTF-8 编码 — 按字节数截取有效位
43+
switch (_Num_bytes) {
44+
case 2:
45+
_Val &= 0b1'1111u; // 2 字节: 取低 5 位
46+
break;
47+
case 3:
48+
_Val &= 0b1111u; // 3 字节: 取低 4 位
49+
break;
50+
case 4:
51+
_Val &= 0b111u; // 4 字节: 取低 3 位
52+
}
53+
```
54+
55+
UTF-8 编码中不同长度字符的有效数据位数不同 (5/4/3 位), 二进制字面量让每一位掩码都对应文档规范中的位布局, 十进制 `31` / `15` / `7` 则完全丢失这层语义
56+
57+
## 三、注意事项
58+
59+
- 二进制字面量只能用于整数类型
60+
- 可以和数字分隔符组合: `0b1010'1100`
61+
- 前缀大小写均可, 推荐 `0b` (更常见)
62+
63+
## 四、练习代码
64+
65+
### 练习代码主题
66+
67+
- 0 - [二进制字面量 — 位掩码运算](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/cpp14/05-binary-literals-0.cpp)
68+
69+
### 练习代码自动检测命令
70+
71+
```
72+
d2x checker binary-literals
73+
```
74+
75+
## 五、其他
76+
77+
- [交流讨论](https://forum.d2learn.org/category/20)
78+
- [d2mcpp教程仓库](https://github.com/mcpp-community/d2mcpp)
79+
- [教程视频列表](https://space.bilibili.com/65858958/lists/5208246)
80+
- [教程支持工具-xlings](https://github.com/openxlings/xlings)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// file: dslings/cpp14/05-binary-literals-0.cpp
4+
//
5+
// Exercise/练习: cpp14 | 05 - binary literals | 二进制字面量位掩码
6+
//
7+
// Tips/提示:
8+
// - 0b / 0B 前缀表示二进制字面量
9+
// - 位掩码用二进制表示最直观
10+
//
11+
// Docs/文档:
12+
// - https://en.cppreference.com/w/cpp/language/integer_literal
13+
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp14/05-binary-literals.md
14+
//
15+
// 练习交流讨论: http://forum.d2learn.org/category/20
16+
//
17+
// Auto-Checker/自动检测命令:
18+
//
19+
// d2x checker binary-literals
20+
//
21+
22+
#include <d2x/cpp/common.hpp>
23+
24+
constexpr unsigned READ = 0b001;
25+
constexpr unsigned WRITE = D2X_YOUR_ANSWER;
26+
constexpr unsigned EXEC = 0b100;
27+
28+
bool has_perm(unsigned perm, unsigned flag) { return perm & flag; }
29+
30+
int main() {
31+
32+
unsigned p1 = 0b101;
33+
d2x_assert(has_perm(p1, READ));
34+
d2x_assert(!has_perm(p1, WRITE));
35+
d2x_assert(has_perm(p1, EXEC));
36+
37+
unsigned p2 = D2X_YOUR_ANSWER;
38+
d2x_assert(has_perm(p2, READ));
39+
d2x_assert(has_perm(p2, WRITE));
40+
d2x_assert(has_perm(p2, EXEC));
41+
42+
d2x_assert_eq(0b1111, D2X_YOUR_ANSWER);
43+
44+
D2X_WAIT
45+
46+
return 0;
47+
}

dslings/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0")
99
target("cpp14-00-generic-lambdas-1")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-05-binary-literals
14+
15+
target("cpp14-05-binary-literals-0")
16+
set_kind("binary")
17+
add_files("05-binary-literals-0.cpp")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// file: dslings/en/cpp14/05-binary-literals-0.cpp
4+
//
5+
// Exercise: cpp14 | 05 - binary literals | bit mask operations
6+
//
7+
// Tips:
8+
// - 0b / 0B prefix for binary integer literals
9+
// - Bit masks are most intuitive in binary form
10+
//
11+
// Docs:
12+
// - https://en.cppreference.com/w/cpp/language/integer_literal
13+
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/05-binary-literals.md
14+
//
15+
// Discussion Forum: http://forum.d2learn.org/category/20
16+
//
17+
// Auto-Checker:
18+
//
19+
// d2x checker binary-literals
20+
//
21+
22+
#include <d2x/cpp/common.hpp>
23+
24+
constexpr unsigned READ = 0b001;
25+
constexpr unsigned WRITE = D2X_YOUR_ANSWER;
26+
constexpr unsigned EXEC = 0b100;
27+
28+
bool has_perm(unsigned perm, unsigned flag) { return perm & flag; }
29+
30+
int main() {
31+
32+
unsigned p1 = 0b101;
33+
d2x_assert(has_perm(p1, READ));
34+
d2x_assert(!has_perm(p1, WRITE));
35+
d2x_assert(has_perm(p1, EXEC));
36+
37+
unsigned p2 = D2X_YOUR_ANSWER;
38+
d2x_assert(has_perm(p2, READ));
39+
d2x_assert(has_perm(p2, WRITE));
40+
d2x_assert(has_perm(p2, EXEC));
41+
42+
d2x_assert_eq(0b1111, D2X_YOUR_ANSWER);
43+
44+
D2X_WAIT
45+
46+
return 0;
47+
}

dslings/en/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0")
99
target("cpp14-00-generic-lambdas-1")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-05-binary-literals
14+
15+
target("cpp14-05-binary-literals-0")
16+
set_kind("binary")
17+
add_files("05-binary-literals-0.cpp")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// reference solution for: dslings/cpp14/05-binary-literals-0.cpp
4+
//
5+
// 用途: 仅给 CI 与维护者参考使用,不是教程入口。
6+
// 教程练习入口: dslings/cpp14/05-binary-literals-0.cpp
7+
//
8+
9+
#include <d2x/cpp/common.hpp>
10+
11+
constexpr unsigned READ = 0b001;
12+
constexpr unsigned WRITE = 0b010;
13+
constexpr unsigned EXEC = 0b100;
14+
15+
bool has_perm(unsigned perm, unsigned flag) { return perm & flag; }
16+
17+
int main() {
18+
19+
unsigned p1 = 0b101;
20+
d2x_assert(has_perm(p1, READ));
21+
d2x_assert(!has_perm(p1, WRITE));
22+
d2x_assert(has_perm(p1, EXEC));
23+
24+
unsigned p2 = 0b111;
25+
d2x_assert(has_perm(p2, READ));
26+
d2x_assert(has_perm(p2, WRITE));
27+
d2x_assert(has_perm(p2, EXEC));
28+
29+
d2x_assert_eq(0b1111, 15);
30+
31+
return 0;
32+
}

solutions/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0-ref")
99
target("cpp14-00-generic-lambdas-1-ref")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-05-binary-literals
14+
15+
target("cpp14-05-binary-literals-0-ref")
16+
set_kind("binary")
17+
add_files("05-binary-literals-0.cpp")

0 commit comments

Comments
 (0)