support bazel complie this project and format code.
This commit is contained in:
7
basic_content/bit/BUILD
Normal file
7
basic_content/bit/BUILD
Normal file
@@ -0,0 +1,7 @@
|
||||
# please run `bazel run basic_content/bit:bit`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "bit",
|
||||
srcs = ["bit.cpp"],
|
||||
)
|
@@ -108,7 +108,7 @@ struct stuff
|
||||
|
||||
这里 stuff.field1 与 stuff.field2 之间有一个 2 Bits 的空隙,stuff.field3 则存储在下一个 unsigned int 中,该结构现在大小为 3 * 32 = 96 Bits。
|
||||
|
||||
学习代码见:[learn.cpp](learn.cpp)
|
||||
学习代码见:[bit.cpp](bit.cpp)
|
||||
|
||||
## 位域的初始化和位的重映射
|
||||
|
||||
|
18
basic_content/bit/bit.cpp
Normal file
18
basic_content/bit/bit.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
struct stuff {
|
||||
unsigned int field1 : 30;
|
||||
unsigned int : 2;
|
||||
unsigned int field2 : 4;
|
||||
unsigned int : 0;
|
||||
unsigned int field3 : 3;
|
||||
};
|
||||
int main() {
|
||||
struct stuff s = {1, 3, 5};
|
||||
cout << s.field1 << endl;
|
||||
cout << s.field2 << endl;
|
||||
cout << s.field3 << endl;
|
||||
cout << sizeof(s) << endl;
|
||||
return 0;
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
struct stuff
|
||||
{
|
||||
unsigned int field1: 30;
|
||||
unsigned int : 2;
|
||||
unsigned int field2: 4;
|
||||
unsigned int : 0;
|
||||
unsigned int field3: 3;
|
||||
};
|
||||
int main(){
|
||||
struct stuff s={1,3,5};
|
||||
cout<<s.field1<<endl;
|
||||
cout<<s.field2<<endl;
|
||||
cout<<s.field3<<endl;
|
||||
cout<<sizeof(s)<<endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user