- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read a specific bit of a number with Arduino
Each number has a specific binary representation. For example, 8 can be represented as 0b1000, 15 can be represented as 0b1111, and so on. If you wish to read a specific bit of a number, Arduino has an inbuilt method for it.
Syntax
bitRead(x, index)
where, x is the number whose bits you are reading, index is the bit to read. 0 corresponds to least significant (right-most) bit, and so on.
This function returns either 0 or 1 depending on the value of that bit in that number.
Example
The following example will illustrate the use of this function −
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int x = 8; Serial.println(bitRead(x,0)); Serial.println(bitRead(x,1)); Serial.println(bitRead(x,2)); Serial.println(bitRead(x,3)); Serial.println(bitRead(x,4)); Serial.println(bitRead(x,5)); Serial.println(bitRead(x,6)); Serial.println(bitRead(x,7)); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is shown below −
As you can see, only the bit in position 3 is 1, while all others are 0, which corresponds to the binary representation of 8: 0b00001000
- Related Articles
- Clear/Set a specific bit of a number in Arduino
- Map a 10-bit number to 8-bit in Arduino
- Find if a string begins with a specific set of characters in Arduino
- How to interface Arduino with a GSM Module and read SMS?
- Digital Read in Arduino
- Read a file from SD Card connected to Arduino
- How to interface Arduino with a GSM module and delete all the read SMSes?
- 8085 Program to Divide a 16-bit number by an 8-bit number
- 8086 program to divide a 16 bit number by an 8 bit number
- How to read a Specific Line From a File in Linux?
- Interfacing a speaker with Arduino
- Get value of the bit at a specific position in BitArray in C#
- Set characters at a specific position within the string in Arduino
- Split a range of number to a specific number of intervals JavaScript
- MySQL RegExp to fetch records with only a specific number of words

Advertisements