Boolean Vs. Bitwise

Can somebody please explain to me in laymen term whats the different between boolean and bitwise. I have read some but don't understand much.

Bitwise operators get and set the values of individual bits. Arduino functions like bitRead, bitWrite, bitSet and bitClear are bitwise operators

Boolean operators perform mathematical operations on values. Boolean operators are: && (boolean and) , || (boolean or), ! (boolan not).
x && y returns true only if both x and y are true
x || y returns true if either x or y are true.

There is a really good tutorial covering bit math and the do's and dont's by CosineKitty.

It cleared alot up for me (had a hard time understand bit math before I read it).

Highly recommended.

1 Like

To add to what mem said:

&& and || are a logical operators for AND and OR
& and | are a bitwise operators for AND and OR

mem explained the logical operators. They are used to compare logical operations - true and false states. For example:
if(a> 3 && b <= 88)
{
//The code here will execute if a is greater than 3 AND
// b is less than or equal to 88
}

Bitwise operators work at the bit level. Here's a quick tutorial on bitwise operators. There are 6 of them.

Let's start with these variables
uint8_t a = 0x3B //00111011;
uint8_t b = 0x96 //10010110;

We will start with AND, OR, XOR, and NOT. These operators compare each bit between two values.

AND (& in C):

AND compares each bit and returns 1 if the two bits are 1 (true) otherwise 0 (false).
So:
c = a & b;

c's value will be 0x12 or in binary 00010010. That is where there is a 1 in each bit for both values.

OR (| in C)
OR will return a 1 if there is a 1 in either value at that bit.
So:
c = a | b;

c's value will be 0xBF or in binary 10111111

XOR (^ in C - pronounced exclusive OR):
XOR will return a 1 if there is a 1 at that bit for either value but not both. For example, if bit position 5 for both values has a 1 then XOr returns a 0. But if only one has a 1 and the other has a 0 XOR returns a 1.
So:
c = a^b;

c's value is 0xAD - binary 10101101

NOT(! in C)
This returns the compliment of a value. This mean where there was a 0 there will now be a 1 and visa versa.
So:
c = ~a;

c's value will be 0xC4 - binary 11000100

Sometimes it is easier to visualize the values if they are above each other, like:

  00111011
& 10010110
----------
= 00010010

  00111011
| 10010110
----------
= 10111111

  00111011
^ 10010110
----------
= 10101101

 !00111011
----------
= 11000100

And then there are two shift operators - left shift and right shift. These shift the bits by the corresponding value - in otherwords move the bits over. << for left shift and >> for right shift.
So:
c = a << 2; // left shift a by 2
d = b >> 4; // right shift b by 4

c's value is 0xEC - binary 11101100
d's value is 0x03 - binary 00001001

1 Like

Why would you use bitwise operators? It can save memory and it is fast.

Let's say you have 8 LEDs, each connected to a pin on the arduino. This means one the first pass the first LED is on and the rest are off. On the next pass the second LED is one and the rest are off. On the third pass the third LED is on and the rest are off. And so forth, repeating over and over.

To do this you will need to keep track of the state of each LED so you know if you need to turn it on or off. Many new programmers would make an array of integers like int led_states[8]; Then set each array value to HIGH or LOW. There's a lot of wasted space there since we just need to know 1 or 0. We can store the information in a single uint8_t value where each bit is the state of the corresponding LED.

To make a chase animation you could do something like this (assuming the LEDs are pin 5-13):
Note: this is still some sloppy code, I know some are saying it can be written better. I wrote it this way so it is obvious what is going on.

uint8_t led_state = 0;   //start state of 00000000 in binary

void loop()
{
      //shift value to the left one space
      //For example 00000001 becomes 00000010
      led_state <<= 1;

      //check if we shifted the 1 too far
      if(led_state == 0)
            led_state = 1;  //reset to 00000001
      
      //update the LEDs
      for(int i = 0; i < 8; i++)
      {
            //First shift 1 to the left to the LED we are checking
            //then AND that result with the the led_state to filter that bit
            //then shift back to the right so we have either 1 or 0 (HIGH or LOW)
            digitalWrite(5+i; ((led_state & (1 << 1)) >> i));
      }
}
1 Like

A Boolean expression is used when you want to know something about a variable, is it equal to 12, is it greater then 123, is it less then 7, etc.

A bitwise expression is used when you want to modifiy a variable by changing some or any of the individual bits of the variable.

Lefty

1 Like