[英]How to store binary input in a C++ array
I understand I can use the library bitset to handle binary input and operations on it. 我明白我可以使用位集来处理它的二进制输入和库操作。
I want to xor certain bits on the input and perform shifting in the binary sequence the user has entered. 我想对输入中的某些位进行异或运算,并按照用户输入的二进制顺序执行移位 。
I think it can be done in an array, but how can I put each bit in an array element? 我认为可以在数组中完成操作,但是如何将每个位放在数组元素中呢?
An example will be really helpful. 一个例子将非常有帮助。
You can operate directly on the std::bitset
as if it were an array, because the []
operator is conveniently overloaded for you, eg 您可以直接在std::bitset
对其进行操作,就好像它是一个数组一样,因为[]
运算符为您方便地重载了,例如
std::bitset a, b, c;
for (i = 0; i < a.size(); ++i)
c[i] = a[i] ^ b[i]; // c = a XOR b
(Note: this assumes that a
, b
and c
all have the same size.) (注意:这假设a
, b
和c
都具有相同的大小。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.