& (Bitwise AND)

From Microduino Wiki
Revision as of 02:51, 11 August 2016 by Fengfeng (talk) (Created page with "*'''Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)''' Bitwise and(&) Bitwise operators operate bit-level calculation to variables. They can resolve many general program...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  • 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 cover for debugging
delay(100);
}
}


[Return to Arduino Syntax Manual]