From 5d06581f82a99407505cdcf28dd2f90d4400ad8b Mon Sep 17 00:00:00 2001 From: feilong Date: Sat, 23 Oct 2021 21:45:37 +0800 Subject: [PATCH] add bit filed --- .../config.json" | 5 +- .../pack01.json" | 5 ++ .../pack01.md" | 5 +- .../pack02.json" | 5 ++ .../pack02.md" | 49 ++++++++++++++++ .../bit_field.c" | 10 ++++ .../bit_field.json" | 5 ++ .../bit_field.md" | 57 +++++++++++++++++++ .../config.json" | 4 +- .../pack01.json" | 0 10 files changed, 141 insertions(+), 4 deletions(-) rename "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" => "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" (69%) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" rename "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" => "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" (85%) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.md" rename "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\344\275\215\345\255\227\346\256\265/config.json" => "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/config.json" (76%) delete mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" similarity index 69% rename from "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" rename to "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" index 8f39d59..0e6521a 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/config.json" @@ -6,5 +6,8 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "pack01.json", + "pack02.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" new file mode 100644 index 0000000..c14c57a --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "pack01.md" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" similarity index 85% rename from "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" rename to "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" index 96a74b9..ce7f0e3 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.md" @@ -1,4 +1,4 @@ -# 结构体位对齐 +# 结构体位对齐(1) 假设机器字是8 bytes,下面代码的占据的内存大小是多大? ```c @@ -14,13 +14,14 @@ struct Test{ ## template ```c +#include struct Test{ char *p; char c; int x; }; int main(int argc, char**){ - print("%d", sizeof(struct Test)) + printf("%d", sizeof(struct Test)); return 0; } ``` diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" new file mode 100644 index 0000000..e603144 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "pack02.md" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" new file mode 100644 index 0000000..8e4f3a0 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\345\257\271\351\275\220\347\211\271\346\200\247/pack02.md" @@ -0,0 +1,49 @@ +# 结构体位对齐(2) + +结构体需要与其最宽的成员为基准对齐,假设机器字是8 bytes,下面代码的占据的内存大小是多大? +```c +struct foo3 { + char *p; + char c; +}; +``` + +## template + +```c +#include +struct foo3 { + char *p; + char c; +}; +int main(int argc, char**){ + printf("%d", sizeof(struct foo3)); + return 0; +} +``` + +## 答案 + +```c +16 bytes +``` + +## 选项 + +### 选项1 + +```c +9 bytes +``` + +### 选项2 + +```c +12 bytes +``` + +### 选项3 + +```c +8 bytes +``` \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.c" new file mode 100644 index 0000000..b6653f8 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.c" @@ -0,0 +1,10 @@ +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char **argv) { + printf("%lu", sizeof(struct foo5)); + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.json" new file mode 100644 index 0000000..7eba2ba --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "bit_field.md" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.md" new file mode 100644 index 0000000..9133d49 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/bit_field.md" @@ -0,0 +1,57 @@ +# 十六进制转换 + +利用位域,你能声明比字符宽度更小的成员,低至1位。假设机器字是8 bytes,下面的结构体大小是? + +```c +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char **argv) { + printf("%lu", sizeof(struct foo5)); + return 0; +} +``` + +## template + +```c +#include +struct foo5 { + int flip : 1; + int nybble : 4; + int septet : 7; +}; +int main(int argc, char**){ + printf("%lu", sizeof(struct foo5)); + return 0; +} +``` + +## 答案 + +```c +4 +``` + +## 选项 + +### 选项1 + +```c +16 +``` + +### 选项2 + +```c +32 +``` + +### 选项3 + +```c +8 +``` \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\344\275\215\345\255\227\346\256\265/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/config.json" similarity index 76% rename from "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\344\275\215\345\255\227\346\256\265/config.json" rename to "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/config.json" index d31d13e..f162213 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/6.\344\275\215\345\255\227\346\256\265/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\344\275\215\345\255\227\346\256\265/config.json" @@ -6,5 +6,7 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "bit_filed.json" + ] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/7.\345\257\271\351\275\220\347\211\271\346\200\247/pack01.json" deleted file mode 100644 index e69de29..0000000 -- GitLab