bitRead()/bitSet()/bitClear()/bitWrite()

Abstract

These macros read and write a bit from/to in the variable.

Source Code

The bitRead()/bitSet()/bitClear()/bitWrite() are defined in hardware/arduino/avr/cores/arduino/Arduino.h as below.

1
2
3
4
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))

The bitRead() shifts right the value by bit-bits, then get bitwise AND with 0x01. So it gets the bit-th bit of the value.

The bitSet() shifts left 0x01 by bit-bits, then get bitwise OR with value. So it sets the bit-th bit of the value to 1.

The bitClear() shift left 0x01 by bit-bits and inverts all the bits, then get bitwise AND with value. So it sets the bit-th bit of the value to 0.

The bitWrite() calls bitSet() if the bitvalue is not 0, or calls bitClear() if the bitvalue is 0.

Version

Arduino AVR Boards 1.8.6

Last Update

March 21, 2023

inserted by FC2 system