Skip to content

Latest commit

 

History

History
75 lines (53 loc) · 1.26 KB

File metadata and controls

75 lines (53 loc) · 1.26 KB
title categories subCategories
bitRead()
Functions
Bits and Bytes

bitRead()

Description

Reads a bit of a number.

Syntax

bitRead(x, n)

Parameters

x: the number from which to read.
n: which bit to read, starting at 0 for the least-significant (rightmost) bit.

Returns

The value of the bit (0 or 1).

Example Code

Reads the bit at the specified position and returns the value. In this example the binary representation of 6 is 0110, since n=1 the value of the second bit from the right (which is 1 in this case) will be read and printed.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int x = 6; int n = 1;       //setting the value of x and n
  Serial.print(bitRead(x,n)); //reading the bit at position n and printing the value
}

Notes and Warnings

The indexing of the rightmost bit starts from 0, so n=1 reads the second bit from the right.

Both x and n must be integer type.

See also