“按位或”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
第1行: 第1行:
 
*'''Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)'''
 
*'''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:
  
位操作符与在C + +中是一个&符,用在两个整型变量之间。按位与运算符对两侧的变量的每一位都进行运算,规则是:如果两个运算元都是1,则结果为1,否则输出0.另一种表达方式:
+
0 0 1 1 operand 1
 
+
0 1 0 1 operand 2
0 0 1 1 运算元1
 
0 1 0 1 运算元2
 
 
----------
 
----------
0 0 0 1(运算元1&运算元2)-返回结果
+
0 0 0 1(operand 1& operand 2)- return the result
在Arduino中,int类型为16位,所以在两个int表达式之间使用&会进行16个并行按位与计算。代码片段就像这样:
+
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">
 
<pre style="color:green">
     int a =  92;    //二进制: 0000000001011100
+
     int a =  92;    //Binary: 0000000001011100
     int b = 101;    // 二进制:0000000001100101
+
     int b = 101;    // Binary:0000000001100101
     int c = a & b;  // 结果:  0000000001000100, 或10进制的68
+
     int c = a & b;  // Result:  0000000001000100, or decimal 68
 
</pre>
 
</pre>
  
a和b的16位每位都进行按位与计算,计算结果存在c中,二进制结果是01000100,十进制结果是68.
+
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(|)
  
按位或操作符在C++中是|。和&操作符类似,|操作符对两个变量的为一位都进行运算,只是运算规则不同。按位或规则:只要两个位有一个为1则结果为1,否则为0。换句话说:
+
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 运算元1
+
0 0 1 1 Operand 1
0 1 0 1 运算元2
+
0 1 0 1 Operand 2
 
----------
 
----------
0 1 1 1(运算元1 | 运算元2) - 返回的结果
+
0 1 1 1(operand 1 | operand 2) - The returned result
这里是一个按位或运算在C + +代码片段:
+
There is a code fragment of bitwise or operation in C + +
 
<pre style="color:green">
 
<pre style="color:green">
     int a =  92;    // 二进制: 0000000001011100
+
     int a =  92;    // Binary: 0000000001011100
     int b = 101;    //二进制: 0000000001100101
+
     int b = 101;    //Binary: 0000000001100101
     int c = a | b;  // 结果:    0000000001111101, 或十进制的125
+
     int c = a | b;  // Result:    0000000001111101, or decimal 125
 
</pre>
 
</pre>
  
示例程序
+
Sample program
  
按位与和按位或运算常用于端口的读取-修改-写入。在微控制器中,一个端口是一个8位数字,它用于表示引脚状态。对端口进行写入能同时操作所有引脚。
+
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是一个内置的常数,是指0,1,2,3,4,5,6,7数字引脚的输出状态。如果某一位为1,着对应管脚为HIGH。(此引脚需要先用pinMode()命令设置为输出)因此如果我们这样写,PORTD=B00110001;则引脚2、3、7状态为HIGH。这里有个小陷阱,我们可能同时更改了引脚0、1的状态,引脚0、1是Arduino串行通信端口,因此我们可能会干扰通信。
+
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 :
  
读取PORT并用按位与清除我们想要控制的引脚
+
Read PORT, and clear the pins we want with bitwise and.
  
用按位或对PORTD和新的值进行运算
+
用按位或Calculate PORTD and the new value with bitwise or.
 
<pre style="color:green">
 
<pre style="color:green">
int i;    // 计数器
+
int i;    // Counter
 
int j;
 
int j;
 
   
 
   
 
void setup()
 
void setup()
DDRD = DDRD | B11111100; //设置引脚2~7的方向,0、1脚不变(xx|00==xx)
+
DDRD = DDRD | B11111100; //Set the direction of pin 2~7, and keep 0 and 1 unchanged(xx|00==xx).
//效果和pinMode(pin,OUTPUT)设置2~7脚为输出一样
+
//The effect is same to pinMode(pin,OUTPUT)’s setting pin 2~7 as output.
 
serial.begin(9600);
 
serial.begin(9600);
 
}
 
}
第61行: 第60行:
 
for (i=0; i<64; i++){
 
for (i=0; i<64; i++){
 
   
 
   
PORTD = PORTD & B00000011;  // 清除2~7位,0、1保持不变(xx & 11 == xx)
+
PORTD = PORTD & B00000011;  // clear 2~7 bits, and keep 0, 1 unchanged(xx & 11 == xx)
j = (i << 2);              //将变量左移为·2~7脚,避免0、1脚
+
j = (i << 2);              //Shift the variable to pin ·2~7, avoiding pin 0 and 1.
PORTD = PORTD | j;          //将新状态和原端口状态结合以控制LED脚
+
PORTD = PORTD | j;          //Combine the new state with the original state of the port to control LED pin.
Serial.println(PORTD, BIN); // 输出掩盖以便调试
+
Serial.println(PORTD, BIN); // output mask for debugging
 
delay(100);
 
delay(100);
 
}
 
}
第71行: 第70行:
  
  
[[https://www.microduino.cn/wiki/index.php/Arduino_%E8%AF%AD%E6%B3%95%E6%89%8B%E5%86%8C/zh 返回Arduino语法手册]]
+
[[https://wiki.microduino.cc/index.php/Arduino_Syntax_Manual Return to Arduino Syntax Manual]]

2016年8月11日 (四) 02:53的版本

  • 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:

    int a =  92;    //Binary: 0000000001011100
    int b = 101;    // Binary:0000000001100101
    int c = a & b;  // Result:  0000000001000100,  or decimal 68

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 + +:

    int a =  92;    // Binary: 0000000001011100
    int b = 101;    //Binary: 0000000001100101
    int c = a | b;  // Result:    0000000001111101,  or decimal 125

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.

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);
}
}


[Return to Arduino Syntax Manual]