结构到位 C++
Struct to bits c++
我正在学习 C++,我想知道是否可以将一个结构对象分解成一个位序列?
// The task is this! I have a structure
struct test {
// It contains an array
private:
int arr [8];
public:
void init () {
for (int i = 0; i <8; i ++) {
arr [i] = 5;
}
}
};
int main () {
// at some point this array is initialized
test h;
h.init ();
// without referring to the arr field and its elements, we must convert the structure to this format
// we know that int is stored there, and these are 32 bits -> 00000000 00000000 00000000 00000101. 00000000 00000000 00000000 00000101. - and there are 8 such pieces by number
// elements in the array
return -1;
}
嗯,我们也知道数组的大小。我们需要将结构对象转换为位序列:
00000000000000000000000000000101000000000000000000000000000001010000000000000000000000000000010100000000000000000000000000000101000000000000000000000000000001010000000000000000000000000000010100000000000000000000000000000000010100000000000000000000000000000101
将数字转换为位串的标准答案是给你的std::bitset
。我将使用更底层的方法。并使用位掩码和 & 操作来屏蔽掉单个位,然后将相应的字符分配给结果字符串。
使用位和运算符以及局部 AND 运算屏蔽工作
Bit Mask AND
0 0 0
0 1 0
1 0 0
1 1 1
你看,0和1是0。1和1是1。
这允许我们访问一个字节中的一个位。
Byte: 10101010
Mask: 00001111
--------------
00001010
我们将使用这个机制。
但是,我无法想象这是家庭作业,因为需要通过从外部访问结构来进行肮脏的 reintepret_cast
。
无论如何。让我向您展示这个解决方案。
我觉得非常丑。
#include <iostream>
#include <bitset>
// The task is this! I have a structure
struct test {
// It contains an array
private:
int arr[8];
public:
void init() {
for (int i = 0; i < 8; i++) {
arr[i] = 5;
}
}
};
// Convert an int to a string with the bit representaion of the int
std::string intToBits(int value) {
// Here we will store the result
std::string result{};
// We want to mask the bit from MSB to LSB
unsigned int mask = 1<<31;
// Now we will work on 4 bytes with 8bits each
for (unsigned int byteNumber = 0; byteNumber < 4; ++byteNumber) {
for (unsigned int bitNumber = 0; bitNumber < 8; ++bitNumber) {
// Mask out bit and store the resulting 1 or 0 in the string
result += (value & mask) ? '1' : '0';
// Next mask
mask >>= 1;
}
// Add a space between bytes
result += ' ';
}
// At the end, we do want to have a point
result.back() = '.';
return result;
}
int main() {
// At some point this array is initialized
test h;
h.init();
// Now do dirty, ugly, and none compliant type cast
int* test = reinterpret_cast<int*>(&h);
// Convert all bytes and show result
for (unsigned int k = 0; k < 8; ++k)
std::cout << intToBits(test[k]) << ' ';
return 0;
}
我正在学习 C++,我想知道是否可以将一个结构对象分解成一个位序列?
// The task is this! I have a structure
struct test {
// It contains an array
private:
int arr [8];
public:
void init () {
for (int i = 0; i <8; i ++) {
arr [i] = 5;
}
}
};
int main () {
// at some point this array is initialized
test h;
h.init ();
// without referring to the arr field and its elements, we must convert the structure to this format
// we know that int is stored there, and these are 32 bits -> 00000000 00000000 00000000 00000101. 00000000 00000000 00000000 00000101. - and there are 8 such pieces by number
// elements in the array
return -1;
}
嗯,我们也知道数组的大小。我们需要将结构对象转换为位序列:
00000000000000000000000000000101000000000000000000000000000001010000000000000000000000000000010100000000000000000000000000000101000000000000000000000000000001010000000000000000000000000000010100000000000000000000000000000000010100000000000000000000000000000101
将数字转换为位串的标准答案是给你的std::bitset
。我将使用更底层的方法。并使用位掩码和 & 操作来屏蔽掉单个位,然后将相应的字符分配给结果字符串。
使用位和运算符以及局部 AND 运算屏蔽工作
Bit Mask AND
0 0 0
0 1 0
1 0 0
1 1 1
你看,0和1是0。1和1是1。
这允许我们访问一个字节中的一个位。
Byte: 10101010
Mask: 00001111
--------------
00001010
我们将使用这个机制。
但是,我无法想象这是家庭作业,因为需要通过从外部访问结构来进行肮脏的 reintepret_cast
。
无论如何。让我向您展示这个解决方案。
我觉得非常丑。
#include <iostream>
#include <bitset>
// The task is this! I have a structure
struct test {
// It contains an array
private:
int arr[8];
public:
void init() {
for (int i = 0; i < 8; i++) {
arr[i] = 5;
}
}
};
// Convert an int to a string with the bit representaion of the int
std::string intToBits(int value) {
// Here we will store the result
std::string result{};
// We want to mask the bit from MSB to LSB
unsigned int mask = 1<<31;
// Now we will work on 4 bytes with 8bits each
for (unsigned int byteNumber = 0; byteNumber < 4; ++byteNumber) {
for (unsigned int bitNumber = 0; bitNumber < 8; ++bitNumber) {
// Mask out bit and store the resulting 1 or 0 in the string
result += (value & mask) ? '1' : '0';
// Next mask
mask >>= 1;
}
// Add a space between bytes
result += ' ';
}
// At the end, we do want to have a point
result.back() = '.';
return result;
}
int main() {
// At some point this array is initialized
test h;
h.init();
// Now do dirty, ugly, and none compliant type cast
int* test = reinterpret_cast<int*>(&h);
// Convert all bytes and show result
for (unsigned int k = 0; k < 8; ++k)
std::cout << intToBits(test[k]) << ' ';
return 0;
}