查看“按位或”的源代码
←
按位或
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
*'''Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)''' Bitwise and(&) Bitwise operators operate bit-level calculation to variables. They can resolve many general programming problems. Bitwise operator “and” is & in C + +, which is used between two int variables. Bitwise “and” operator calculates each bit of the two operands on both sides, and the rule is:if both operands are 1, the result is 1, otherwise input 0. The other expression: 0 0 1 1 operand 1 0 1 0 1 operand 2 ---------- 0 0 0 1(operand 1& operand 2)- return the result In Arduino,type int is 16-bit, so & used between two int expressions will operate 16 parallel bitwise calculation. The code fragment is like this: <pre style="color:green"> int a = 92; //Binary: 0000000001011100 int b = 101; // Binary:0000000001100101 int c = a & b; // Result: 0000000001000100, or decimal 68 </pre> Each of the 16 bits of a and b operates bitwise and calculation, and the result is stored in c,. The binary result is 01000100, and decimal result is 68. The most common effect of bitwise and is to choose specific bit from integer variables, namely the bit masking, and you can see it in the example delow. Bitwise or(|) Biwwise or operator is | in C++. Similar with operator &,operator | calculates each bit or both variables, just with different rules. Rules of bitwise or:only if one of the two bits is 1, the result is 1, otherwise it is 0. In other words: 0 0 1 1 Operand 1 0 1 0 1 Operand 2 ---------- 0 1 1 1(operand 1 | operand 2) - The returned result There is a code fragment of bitwise or operation in C + +: <pre style="color:green"> int a = 92; // Binary: 0000000001011100 int b = 101; //Binary: 0000000001100101 int c = a | b; // Result: 0000000001111101, or decimal 125 </pre> Sample program Bitwise and and bitwise or operations are generally used in reading-modifying-writing or ports. In the microcontroller, a port is a 8-bit number, which is used to represent the state of pins. Writing to the ports can operate all pins simultaneously. PORTD is a built-in constant, which refers to the output state of 0, 1, 2, 3, 4, 5, 6, 7 digital pins. If one is 1, the corresponding pin is HIGH.(This pin need to be set as output state with command pinMode() at first). So if we write as this, PORTD=B00110001, the state of pin 2, 3, and 7 is HIGH. There is a small trap that we may change the states of pin 0 and 1 simultaneously, because they are Arduino serial communication ports, we may interfere the communication. Our algorithm program is : Read PORT, and clear the pins we want with bitwise and. 用按位或Calculate PORTD and the new value with bitwise or. <pre style="color:green"> int i; // Counter int j; void setup() DDRD = DDRD | B11111100; //Set the direction of pin 2~7, and keep 0 and 1 unchanged(xx|00==xx). //The effect is same to pinMode(pin,OUTPUT)’s setting pin 2~7 as output. serial.begin(9600); } void loop () { for (i=0; i<64; i++){ PORTD = PORTD & B00000011; // clear 2~7 bits, and keep 0, 1 unchanged(xx & 11 == xx) j = (i << 2); //Shift the variable to pin ·2~7, avoiding pin 0 and 1. PORTD = PORTD | j; //Combine the new state with the original state of the port to control LED pin. Serial.println(PORTD, BIN); // output mask for debugging delay(100); } } </pre> [[https://wiki.microduino.cc/index.php/Arduino_Syntax_Manual Return to Arduino Syntax Manual]]
返回至
按位或
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息