How to Set, Clear and Check a Bit, or Bitwise Operations

For those who need to refresh their knowledge, I'll leave a cheat sheet here; these operations will be discussed in more detail in the article.

set bit zero
PORTB | = 0x01;

clear bit zero
PORTB &= ~0x01;

check whether a bit is set
if(PORTB & 0x01)
{

}

invert the value of bit zero
#define    LED    0x01 
PORTB ^= LED;


Regardless of which microcontrollers you're going to program, the first thing you'll have to master is bitwise operations.
There are only 6 bitwise operations in C.

& ( AND )
| ( OR )
^ ( XOR )
~  ( NOT )
<<(shift left)
>>(shift right)

Let's start with the fact that microcontroller pins are grouped into ports; on the Atmega16 a port consists of 8 pins, on the STM32f103 of 16 pins.
How to Set, Clear and Check a Bit, or Bitwise Operations

The marked pins make up port A.
Bitwise OR — the result of the operation is 1 if either of the corresponding bits is 1, otherwise 0.
How to Set, Clear and Check a Bit, or Bitwise Operations

You can set bit zero of port B to 1 as follows.

PORTB = 0x01; //hexadecimal notation
or
PORTB = 0b00000001; //binary notation
or
PORTB = 1; //decimal notation

This way we set bit zero to 1 and all the others to 0, i.e. we overwrote all the bits of the port. But what if we want to set only bit zero to 1 without affecting the others? In that case we need to use bitwise OR.


set bit zero to one
PORTB = PORTB | 0x01;
or using compound assignment
PORTB | = 0x01;

How to Set, Clear and Check a Bit, or Bitwise Operations

As a result, we changed only bit zero of the port.
It should be noted that in microcontrollers counting starts from zero, i.e. the first bit has index zero, and the eighth bit has index 7.


The bitwise NOT operation inverts the value of a bit.
How to Set, Clear and Check a Bit, or Bitwise Operations

Bitwise AND — if the corresponding bits are both 1, the resulting bit is 1. If either of the corresponding bits is 0, the resulting bit is 0.
How to Set, Clear and Check a Bit, or Bitwise Operations

This operation, together with bitwise NOT, can be used to clear a specific bit to zero.


clear bit zero
PORTB &= 0xFE;
or
PORTB &= ~0x01;

With this notation we set to one the bit we want to clear, then invert the resulting number
How to Set, Clear and Check a Bit, or Bitwise Operations

Now we apply the resulting mask
How to Set, Clear and Check a Bit, or Bitwise Operations

As a result, we set only the first bit to 0.

This operation can also be used to check the value of a bit. For example, we need to check the value of bit zero of port B; this can be done with the following construct.
if(PORTB & 0x01)

How to Set, Clear and Check a Bit, or Bitwise Operations

If the bit is one, the expression in parentheses will be true; otherwise, false.


Bitwise exclusive OR — if the sum of the corresponding bits is even, the resulting bit is 0, otherwise 1.
How to Set, Clear and Check a Bit, or Bitwise Operations

With this operation you can invert the state of a selected bit. For example, an LED is connected to pin zero of the port, and when the same piece of code is executed, we want it to turn off if it's on and, conversely, turn on if it's off.

#define    LED    0x01 
PORTB ^= LED;

How to Set, Clear and Check a Bit, or Bitwise Operations

This operation can also be used to determine whether registers are equal. For example, we want to compare whether ports B and D are in the same state.
if(PORTB ^ PORTD)

How to Set, Clear and Check a Bit, or Bitwise Operations

If the result is zero, the contents of the registers are equal.


Logical shift left — all bits are shifted one position to the left, the leftmost bit is lost, and 0 is written into the rightmost bit.
How to Set, Clear and Check a Bit, or Bitwise Operations

A logical shift left is equivalent to multiplying by 2.
0b0000 1011 = 11
0b0001 0110 = 22


Logical shift right — all bits are shifted one position to the right, the rightmost bit is lost, and 0 is written into the leftmost bit.
How to Set, Clear and Check a Bit, or Bitwise Operations

A logical shift right is equivalent to dividing by 2.
0b1000 1011 = 147
0b0100 0101 = 73
You can see that when dividing by 2 the result is rounded down; that's all.