It is often necessary to test the status of a single bit. The problem is then to test this bit independently of the others. This page explains how to properly test a single bit isolated from the rest of a binary word.

The principle consists in forcing to zero all the bits except the one you want to test.

Clearing bits to zero is done thanks to a bitwise **AND** between the register and a mask.
The bits of the mask are defined such as:

`0`

in the mask => all bits to**inhibit**;`1`

in the mask => the bit to**check**.

Suppose we want to test the most significant bits of an 8-bit word. The mask is:

$$ mask = (1000~0000)_2 = (80)_{16} $$

$$ \begin{array}[t]{r} b_7 & b_6 & b_5 & b_4 & \, & b_3 & b_2 & b_1 & b_0 & <= & initial \: word \\ \& \quad 1 & 0 & 0 & 0 & \, & 0 & 0 & 0 & 0 & <= & mask \\ \hline b_7 & 0 & 0 & 0 & \, & 0 & 0 & 0 & 0 & <= & result \end{array} $$

By analyzing the result of equation 2, we easily understand that the result can only take two values :

- either \(b_7\) equals 0 and the result is 0
- either \(b_7\) equals 1 and the result is 0x80

To determine the status of the bit, we just need to test the masking result.

```
if ( (registre & 0x80) == 0x80) {
// The most significant bit is 1
// ...
}
else {
// The most significant bit is 0
// ...
}
```

The result of the bit-masking can only return 2 values: `0x80`

and `0`

.
From a purely logical point of view:

`0x80`

is the logical proposition**TRUE**;`0`

is the logical proposition**FALSE**.

The test of the above code can be simplified:

```
if ( registre & 0x80 ) {
// The most significant bit is 1
// ...
}
else {
// The most significant bit is 0
// ...
}
```

In embedded systems, it is often necessary to wait for the change of state of a bit. For example, pressing a button. This can be done on the same principle in a loop:

```
// Waits for the second least significant bit to change to 1
while !(PORTA & 0x02);
```

On the same principle, we can also expect a rising front:

```
// Waits for a rising edge on the second least significant bit
while (PORTA & 0x02); // Waits for the bit to be 1
while !(PORTA & 0x02); // Waits for the bit to change to 0
```

Write a program that asks the user to enter an integer. The program then displays the parity of the number entered. The program must use bit-masking to test the least significant bit.

- Least significant bit equals
**0**=>**even**number. - Least significant bit equals
**1**=>**odd**number.

```
unsigned int N;
printf ("Enter a positive integer ");
scanf("%d", &N);
// Check the least significant bit
// to display parity
// COMPLETE HERE
```

**How to proceed to test a bit independently of the others?**

Check
Bravo! The bit must be isolated by clearing the others bits.
Try again...

**Which tests will be true if the third most significant bit of reg is 1?**

`if ( reg & 0x20 == 1)`

`if ( reg & 0x20 == 0x20)`

`if ( reg & 0x20 != 0)`

Check
Bravo! We want to test if the masking result is equal to

`0x20`

, it can be done in different ways.
Try again...
**Which tests will be true if the third most significant bit of reg is 0?**

`if ( reg & 0x20 )`

`if ( reg & 0x20 == 0)`

`if ( reg & 0x20 == 0x20)`

`if ( !(reg & 0x20) )`

Check
Bravo! Just test if the masking result is zero.
Try again...

**Which loops wait for the least significant bit of reg to go to 0?**

`while (reg & 0x01);`

`while !(reg & 0x01);`

`while (reg & 0x01 == 0);`

`while (reg & 0x01 != 0);`

Check
Bravo! The loop will be executed as long as the bit is equal to one, i.e. the result of the masking is not zero.
Try again...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/22/2022