c++ - 有没有办法定义和发送单个 BIT

标签 c++ arduino

<分区>

想法是将各种 radio 发射模块与模拟线圈发射信号进行比较。

因此需要发送单个比特来与arduino radio 发射模块进行比较。

我已经尝试包含 bitset 并定义如下内容:

std::bitset<1> p(1);

但它仍然有 4 个字节大小。

有没有办法声明一个位?

最佳答案

不,你不能声明单个位。只能是 8 的倍数(又名字节)。在 C++ 中,char 类型的变量大小为 1 个字节。如果您想传输/比较一个位序列,您可以使用 charchar 数组。

char single_byte = 32; // same as 0010 0000
char some_bytes[3] = {8, 254, 1}; // same as 0000 1000 1111 1110 0000 0001

要检查某个位是否设置在特定位置,您可以使用位运算符,如 &

// example:
// check if the second bit of the second byte in some_bytes is set

char filter = 64; // 0100 0000
if (some_bytes[1] & filter) // 1111 1110 & 0100 000 = 0100 0000
// returns true

关于c++ - 有没有办法定义和发送单个 BIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57313478/

相关文章:

Visual Studio 2010 中的 C++ 访问修饰符自动缩进慢慢地让我发疯——它可以改变吗?

c++ - 如何在 clang/c2 中使用 boost asio

c++ - 陷入输入 C++ 的无限循环

c++ - 错误: ‘invoke’ is not a member of ‘std’

node.js - Arduino Socket.io 通信

c++ - 在函数定义中使用引用参数 : perfomance?

python - 编写并保存 python 脚本到 arduino yun RAM

java - 将字符串转换为字节数组,然后再转换回原始字符串

python - 重新连接几次后,通过 USB 端口与 Python 的串行通信停止工作

macos - 触发 mac 应用程序以从声音或其他物理(现实世界)事件开始