Arduino

bitSet() and bitWrite() Function in Arduino Programming

In the Arduino programming language, the bitSet() and bitWrite() function is used to manipulate individual bits within a byte or other data type. In this article, we will explore the bitSet() and bitWrite() function in detail and discuss how it can be used to improve the performance and functionality of your Arduino programs.

What is bitSet() Function in Arduino

The bitSet() function is a built-in function in the Arduino programming language that is used to set a specific bit within a byte or other data type to a value 1.

We use this function where we need to manipulate individual bits within a larger data type, as it allows you to do so quickly and easily.

Syntax

Following syntax will be followed for bitSet() function in Arduino:

bitWrite(byteVar, bitNumber);

 

Or:

bitSet(x,n);

 

In this syntax, “x” represents the variable that you want to modify, and “n” represents the bit that you want to set. The bit position is zero-indexed, so the least significant bit is bit 0, the second least significant bit is bit 1, and so on.

Parameters

This function can take two parameters:

byteVar (x): This is the numerical value whose value we need to set.

byteNumber (n): It describes which bit is to be set. It starts at 0 from the least significant bit.

Return Value

The function returns the modified value of the variable “x”.

Example Code

Following code illustrates the use of bitSet() function in Arduino programming:

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

  byte x = 0b11111101;      // This is binary constant value indicated by 0b prefix
  Serial.print("BEFORE: ");
  Serial.println(x, BIN);   // Prints original binary value equal to 11111101

  bitSet(x, 1);             // this function will set 1 to second bit
  Serial.print("AFTER:  ");
  Serial.println(x, BIN);   // Prints binary number after its 2nd bit set to 1 (11111111)
}

void loop() {    }

 

In this code, the setup() function initializes the serial communication and sets a byte variable x to the binary value 0b11111101. The bitSet() function is used to set the second bit (counting from the right) of x to 1. The original and modified values of x are printed to the Serial monitor.

The loop() function is empty, so the program simply runs once and then stops.

Output

In output we can see a second bit from the left or least significant side is set to 1:

What is bitWrite() Function in Arduino

The bitWrite() function allows you to write a value (0 or 1) to a specific bit in a byte of data. Its syntax, parameters, and return value are as follows:

Syntax

Following syntax will be followed by bitWrite() function in Arduino programming:

bitWrite(byteVar, bitNumber, bitValue);

 

Or:

bitWrite(x, n, b);

 

Parameters

Following are the three parameters which this function takes:

byteVar (x): the byte (value in binary) of data where you want to write the bit.

bitNumber (n): the index (0 to 7) or number from least significant bit that we want to write in the byteVar.

bitValue (b): the value (0 or 1) that you want to write to the specified bit.

Return Value

The bitWrite() function does not return any value but it gives the modified numerical value whose bit is changed.

Example Code

Below given code gives us an idea how the bitWrite() function works in Arduino programming:

void setup() {
  Serial.begin(9600);
  byte x = 0b11111101;   // This is binary constant value indicated by 0b prefix
  Serial.print("BEFORE: ");
  Serial.println(x, BIN); // 11111101

  bitWrite(x, 0, 0);      // this will write 0 to least significant bit(0) of x
  Serial.print("AFTER:  ");
  Serial.println(x, BIN); // 11111100
}
void loop() {
}

 

In this code, the setup() function initializes the serial communication and sets a byte variable x to the binary value 0b11111101. The bitWrite() function is used to write 0 to the least significant bit (bit position 0) of x. The original and modified values of x are printed to the Serial monitor.

The loop() function is empty, so the program simply runs once and then stops.

Output

In output we can see the least significant bit set to 0 from 1:

Difference between bitSet() and bitWrite() Arduino Function

The bitSet() and bitWrite() functions are both used in Arduino programming to manipulate individual bits within a byte of data, but they have some differences.

The bitSet() takes a bit index and a reference to a byte of data and sets the bit at the given index to 1.

For example, bitSet(3, myByte) would set the 4th bit (counting from 0) of myByte to 1. If the bit is already set, bitSet() does nothing.

The bitWrite() function takes a bit index, a byte of data, and a value (either 0 or 1), and sets the bit at the given index to the given value.

For example, bitWrite(myByte, 3, 1) would set the 4th bit of myByte to 1. If the value parameter is 0, “bitWrite” clears the bit at the given index.

In summary, bitSet() is used to set a bit to 1, while bitWrite() is used to set a bit to either 0 or 1.

Conclusion

The bitSet() and bitWrite() both functions can change the individual bits within a variable. However the bitSet() can only set the value to 1 but bitWrite() can overwrite values to either 1 or 0 depending on the defined bit inside the third parameter of the function. For more detailed insight and examples of both read the article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.