[英]Bit masking with hexadecimal in c++
I need to mask my output in binary with hexadecimals variables.我需要用十六进制变量以二进制形式屏蔽我的输出。 Do I need to convert the binary output to hexadecimal (or hexadecimals variables to binary)?
我是否需要将二进制输出转换为十六进制(或将十六进制变量转换为二进制)? Or is there any way in C++ to directly mask them and store it to a new variable?
或者在 C++ 中有什么方法可以直接屏蔽它们并将其存储到一个新变量中?
#Edit : The binary output is stored to a std::bitset
variable. #Edit :二进制输出存储到
std::bitset
变量。
The use of bitset wasn't mentioned in your question, improve on that next time.您的问题中没有提到 bitset 的使用,下次改进。 You need to create a bitmask for the hex value as well.
您还需要为十六进制值创建一个位掩码。 Then you can just & the bitmasks
然后你可以只 & 位掩码
#include <bitset>
#include <iostream>
int main()
{
std::bitset<8> value{ 0x03 };
std::bitset<8> mask{ 0x01 };
std::bitset<8> masked_value = value & mask;
std::cout << value.to_string() << " & " << mask.to_string() << " = " << masked_value.to_string() << "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.