c - 在 C 中合并数组中的位变量

标签 c verilog microcontroller 8051

我正在开发 8051 微 Controller 平台,KEIL 作为 IDE,C 作为编程语言。

我正在尝试创建用于开关功能的位数组。

 sbit P1;
 sbit P2;
 sbit P3;

 buttons [2:0] = P1 | P2 | P3

 switch (buttons[2:0])
  {
    case 0x1: //First button is pressed 
    case 0x2: //Second button is pressed
    case 0x4: //Third is pressed
    default: //Multiple buttons are pressed. Hence invalid

  }

我找不到将位组合成单个实体并将其用于开关功能的方法。

任何人都可以建议以任何可能的方法完成任务。

最佳答案

我终于找到了解决问题的方法。

int buttons=0x00;       
buttons = ((0xFF & !P1) << 2) | ((0xFF & !P2) << 1) | ((0xFF & !P3) << 0);

关于c - 在 C 中合并数组中的位变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018418/

相关文章:

比较 C 中的字符会引发错误

c - strcmp 行为

verilog - 在verilog中包含一个模块

assembly - 学习 PIC 单片机的汇编语言的最佳资源是什么

algorithm - 微 Controller 上短数据的校验和?

c# - 从 C 插件向 Unity3D 发送字节数据的任何正确方法?

verilog - 将层次结构传递到 Verilog 模块

bit-manipulation - Verilog中的Pythonesque位切片?

C、将一个 float 拆分成单个数字

c++ - 将 namespace 添加到具有 C header 的 C++ 实现