如何使用按位运算将 3 个整数值编码为 uint16_t?
How to encode 3 integer values into an uint16_t using bitwise operations?
我想通过按位运算将 3 个无符号整数值存储到 uint16_t
变量中,然后使用按位运算将它们读回。以下是我的程序:
代码:
#include <iostream>
uint16_t Write(unsigned int iVal1, unsigned int iVal2, unsigned int iVal3) {
// iVal1 should go into the first 8 bits [iVal1 value ranges from 0 to 173]
// iVal2 should go into the 6 bits after that [iVal2 value ranges from 0 to 63]
// iVal3 should go into the 2 bits after that [iVal3 value ranges from 0 to 3]
// Is the below way of writing the bits correct?
return (static_cast<uint16_t>(iVal1)<<8) + (static_cast<uint16_t>(iVal2)<<6) + (static_cast<uint16_t>(iVal3)<<2);
}
unsigned int ReadVal1(const uint16_t theNumber) {
// ival1 is the first 8 bits
uint16_t check1 = 255;
return (theNumber>>8)&check1;
}
unsigned int ReadVal2(const uint16_t theNumber) {
// ival2 is the 6 bits after that
uint16_t check2 = 63;
return (theNumber>>3)&check2;
}
unsigned int ReadVal3(const uint16_t theNumber) {
// ival3 is the last 2 bits
uint16_t check3 = 3;
return (theNumber>>1)&check3;
}
int main() {
std::cout << "Main started" << std::endl;
unsigned int iVal1 = 191;
unsigned int iVal2 = 28;
unsigned int iVal3 = 3;
const uint16_t theNumber = Write(iVal1, iVal2, iVal3);
std::cout << "The first 8 bits contain the number: " << ReadVal1(theNumber) << std::endl;
std::cout << "Then after 6 bits contain the number: " << ReadVal2(theNumber) << std::endl;
std::cout << "Then after 2 bits contain the number: " << ReadVal3(theNumber) << std::endl;
}
上面程序中需要编码的3个无符号整数的取值范围如下
`iVal1` ranges from `0 to 173`. So its well within 8 bits.
`iVal2` ranges from `0 to 63`. So its well within 6 bits.
`iVal3` ranges from `0 to 3`. So its well within 2 bits.
问题:
我认为我在函数 Write
中编写值的方式是错误的。正确的做法是什么?
主要是,我正在寻找一个很好的解释,说明使用按位运算的编码是如何工作的,尤其是在我上面的程序目标的上下文中。
我相信我读取函数 ReadVal1
、ReadVal2
和 ReadVal3
中的值的方式是正确的。我已经想出了如何回读看似简单的值的技巧。但是,我不太理解如何使用按位运算正确编码值的逻辑。
C++编译器:
我正在使用 C++11 编译器
整数移位的位数不应该取决于被移位的整数的大小,而是取决于在它之后的所有整数的大小(到正确的)。这里有一些 ASCII 艺术来说明原理:
+---+---+---+---+---+---+---+---+
‖i1 |i1 |i1 |i1 |i1 |i1 |i1 |i1 ‖ 8 bit
‖ 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ‖
+---+---+---+---+---+---+---+---+
_______________________________/
/
| +---+---+---+---+---+---+
| ‖i2 |i2 |i2 |i2 |i2 |i2 ‖ 6 bit
| ‖ 5 | 4 | 3 | 2 | 1 | 0 ‖
| +---+---+---+---+---+---+
| ________/
| /
| |+---+---+
| |‖i3 |i3 ‖ 2 bit
| |‖ 1 | 0 ‖
| |+---+---+
| \ |
|<<(6+2) |<<2 |<<0
v v v
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
‖ F | E | D | C | B | A | 9 | 8 ‖ 7 | 6 | 5 | 4 | 3 | 2 ‖ 1 | 0 ‖
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
‖i1 |i1 |i1 |i1 |i1 |i1 |i1 |i1 ‖i2 |i2 |i2 |i2 |i2 |i2 ‖i3 |i3 ‖
‖ 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ‖ 5 | 4 | 3 | 2 | 1 | 0 ‖ 1 | 0 ‖
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
我想通过按位运算将 3 个无符号整数值存储到 uint16_t
变量中,然后使用按位运算将它们读回。以下是我的程序:
代码:
#include <iostream>
uint16_t Write(unsigned int iVal1, unsigned int iVal2, unsigned int iVal3) {
// iVal1 should go into the first 8 bits [iVal1 value ranges from 0 to 173]
// iVal2 should go into the 6 bits after that [iVal2 value ranges from 0 to 63]
// iVal3 should go into the 2 bits after that [iVal3 value ranges from 0 to 3]
// Is the below way of writing the bits correct?
return (static_cast<uint16_t>(iVal1)<<8) + (static_cast<uint16_t>(iVal2)<<6) + (static_cast<uint16_t>(iVal3)<<2);
}
unsigned int ReadVal1(const uint16_t theNumber) {
// ival1 is the first 8 bits
uint16_t check1 = 255;
return (theNumber>>8)&check1;
}
unsigned int ReadVal2(const uint16_t theNumber) {
// ival2 is the 6 bits after that
uint16_t check2 = 63;
return (theNumber>>3)&check2;
}
unsigned int ReadVal3(const uint16_t theNumber) {
// ival3 is the last 2 bits
uint16_t check3 = 3;
return (theNumber>>1)&check3;
}
int main() {
std::cout << "Main started" << std::endl;
unsigned int iVal1 = 191;
unsigned int iVal2 = 28;
unsigned int iVal3 = 3;
const uint16_t theNumber = Write(iVal1, iVal2, iVal3);
std::cout << "The first 8 bits contain the number: " << ReadVal1(theNumber) << std::endl;
std::cout << "Then after 6 bits contain the number: " << ReadVal2(theNumber) << std::endl;
std::cout << "Then after 2 bits contain the number: " << ReadVal3(theNumber) << std::endl;
}
上面程序中需要编码的3个无符号整数的取值范围如下
`iVal1` ranges from `0 to 173`. So its well within 8 bits.
`iVal2` ranges from `0 to 63`. So its well within 6 bits.
`iVal3` ranges from `0 to 3`. So its well within 2 bits.
问题:
我认为我在函数 Write
中编写值的方式是错误的。正确的做法是什么?
主要是,我正在寻找一个很好的解释,说明使用按位运算的编码是如何工作的,尤其是在我上面的程序目标的上下文中。
我相信我读取函数 ReadVal1
、ReadVal2
和 ReadVal3
中的值的方式是正确的。我已经想出了如何回读看似简单的值的技巧。但是,我不太理解如何使用按位运算正确编码值的逻辑。
C++编译器:
我正在使用 C++11 编译器
整数移位的位数不应该取决于被移位的整数的大小,而是取决于在它之后的所有整数的大小(到正确的)。这里有一些 ASCII 艺术来说明原理:
+---+---+---+---+---+---+---+---+
‖i1 |i1 |i1 |i1 |i1 |i1 |i1 |i1 ‖ 8 bit
‖ 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ‖
+---+---+---+---+---+---+---+---+
_______________________________/
/
| +---+---+---+---+---+---+
| ‖i2 |i2 |i2 |i2 |i2 |i2 ‖ 6 bit
| ‖ 5 | 4 | 3 | 2 | 1 | 0 ‖
| +---+---+---+---+---+---+
| ________/
| /
| |+---+---+
| |‖i3 |i3 ‖ 2 bit
| |‖ 1 | 0 ‖
| |+---+---+
| \ |
|<<(6+2) |<<2 |<<0
v v v
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
‖ F | E | D | C | B | A | 9 | 8 ‖ 7 | 6 | 5 | 4 | 3 | 2 ‖ 1 | 0 ‖
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
‖i1 |i1 |i1 |i1 |i1 |i1 |i1 |i1 ‖i2 |i2 |i2 |i2 |i2 |i2 ‖i3 |i3 ‖
‖ 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 ‖ 5 | 4 | 3 | 2 | 1 | 0 ‖ 1 | 0 ‖
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+