bitWrite()

Description

Writes a bit of a numeric variable.

Syntax

bitWrite(x, n, b)

Parameter Values

  • x: the numeric variable to which to write.
  • n: which bit of the number to write, starting at 0 for the least-significant (rightmost) bit.
  • b: the value to write to the bit (0 or 1).

Return Values

  • Nothing

Example Code

Demonstrates the use of bitWrite by printing the value of a variable to the Serial Monitor before and after the use of bitWrite().

void setup() { Serial.begin(9600); byte x = 0b10000000; // the 0b prefix indicates a binary constant Serial.print("BEFORE: "); Serial.println(x, BIN); // 10000000 bitWrite(x, 0, 1); // write 1 to the least significant bit of x Serial.print("AFTER: "); Serial.println(x, BIN); // 10000001 } void loop() {}

The result in Serial Monitor:

COM6
Send
BEFORE: 10000000 AFTER: 10000001
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES