bitSet()

Description

Sets (writes a 1 to) a bit of a numeric variable.

Syntax

bitSet(x, n)

Parameter Values

  • x: the numeric variable whose bit to set.
  • n: which bit to set, starting at 0 for the least-significant (rightmost) bit.

Return Values

  • Nothing

Example Code

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

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

The result in Serial Monitor:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
BEFORE: 10000000 AFTER: 10000100
Ln 11, Col 1
Arduino Uno on COM15
2

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES