RUI3 (RAK Unified Interface 3) - RAK4631
Bit and Byte

Macros

#define bitRead(value, bit)   (((value) >> (bit)) & 0x01)
 
#define bitSet(value, bit)   ((value) |= (1UL << (bit)))
 
#define bitClear(value, bit)   ((value) &= ~(1UL << (bit)))
 
#define bitWrite(value, bit, bitvalue)   ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
 
#define bit(b)   (1UL << (b))
 
#define lowByte(w)   ((uint8_t) ((w) & 0xff))
 
#define highByte(w)   ((uint8_t) ((w) >> 8))
 

Detailed Description

Macro Definition Documentation

◆ bitRead

#define bitRead (   value,
  bit 
)    (((value) >> (bit)) & 0x01)
Description
Reads a bit of a number
Syntax
bitRead(value, bit);
Parameters
valuethe number from which to read
bitwhich bit to read, starting at 0 for the least-significant (rightmost) bit
Returns
The value of the bit (0 or 1)
Example
void setup() {
  Serial.begin(115200);
  Serial.println("Read the bits of 6(0110)");

  Serial.print("bitRead(6, 0) = ");
  Serial.println(bitRead(6, 0));    //print the first bit of 6

  Serial.print("bitRead(6, 1) = ");
  Serial.println(bitRead(6, 1));    //print the second bit of 6

  Serial.print("bitRead(6, 2) = ");
  Serial.println(bitRead(6, 2));    //print the third bit of 6

  Serial.print("bitRead(6, 3) = ");
  Serial.println(bitRead(6, 3));    //print the fourth bit of 6
}

void loop() {
}

◆ bitSet

#define bitSet (   value,
  bit 
)    ((value) |= (1UL << (bit)))
Description
Sets (writes a 1 to) a bit of a numeric variable
Syntax
bitSet(value, bit);
Parameters
valuethe numeric variable whose bit to set
bitwhich bit to set, starting at 0 for the least-significant (rightmost) bit
Returns
void
Example
void setup() {
  Serial.begin(115200);

  Serial.print("Before bitSet(): 6 => ");
  Serial.println(6, BIN);

  Serial.print("After bitSet(6, 0) => ");
  int value = 6;
  int pos = 0;
  Serial.println(bitSet(value,pos), BIN);  //set the first bit of 6(0110) to 1
}

void loop() {
}

◆ bitClear

#define bitClear (   value,
  bit 
)    ((value) &= ~(1UL << (bit)))
Description
Clears (writes a 0 to) a bit of a numeric variable
Syntax
bitClear(value, bit);
Parameters
valuethe numeric variable whose bit to clear
bitwhich bit to clear, starting at 0 for the least-significant (rightmost) bit
Returns
the value of the numeric variable after the bit at position n is cleared
Example
void setup() {
  Serial.begin(115200);

  Serial.print("Before bitClear(): 6 => ");
  Serial.println(6, BIN);

  Serial.print("After bitClear(6, 1) => ");
  int value = 6;
  int pos = 1;
  Serial.println(bitClear(value,ipos), BIN);  // set the second bit of 6(0110) to 0
}

void loop() {
}

◆ bitWrite

#define bitWrite (   value,
  bit,
  bitvalue 
)    ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
Description
Writes a bit of a numeric variable
Syntax
bitWrite(value, bit, bitvalue);
Parameters
valuethe numeric variable to which to write
bitwhich bit of the number to write, starting at 0 for the least-significant (rightmost) bit
bitvaluethe value to write to the bit (0 or 1)
Returns
void
Example
void setup() {
  Serial.begin(115200);

  int target = 6; // set the bitWrite target to 6
  Serial.print("Before bitWrite(): 6 => ");
  Serial.println(target, BIN);

  Serial.print("After bitWrite(target, 0, 1) => ");  // Set the first bit of target to 1
  Serial.println(bitWrite(target, 0,1), BIN);      

  Serial.print("After bitWrite(target, 1, 0) => ");  // Set the second bit of target to 0
  Serial.println(bitWrite(target, 1,0), BIN);

  Serial.print("After bitWrite(target, 2, 0) => ");  // Set the third bit of target to 0
  Serial.println(bitWrite(target, 2,0), BIN);

  Serial.print("After bitWrite(target, 3, 1) => ");  // Set the fourth bit of target to 1
  Serial.println(bitWrite(target, 3,1), BIN);

  Serial.println("");
  Serial.println("target now should be 9(1001)");
  Serial.print("target = ");
  Serial.println(target);
}

void loop() {
}

◆ bit

#define bit (   b)    (1UL << (b))
Description
Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.)
Syntax
bit(b)
Parameters
bthe bit whose value to compute
Returns
The value of the bit
Example
void setup() {
  Serial.begin(115200);

  Serial.print("bit 0: ");
  Serial.println(bit(0));
  Serial.print("bit 3: ");
  Serial.println(bit(3));
}

void loop() {
}

◆ lowByte

#define lowByte (   w)    ((uint8_t) ((w) & 0xff))
Description
Extracts the low-order (rightmost) byte of a variable (e.g. a word)
Syntax
lowByte(w);
Parameters
wa value of any type
Returns
byte
Example
void setup() {
  Serial.begin(115200);
  Serial.println("Test target 0xABCD");

  Serial.print("lowByte() of 0xABCD = "); // extract the low-order byte of the target
  Serial.println(lowByte(target), HEX);

  Serial.print("highByte() of 0xABCD = "); // extract the low-order byte of the target
  Serial.println(highByte(target), HEX);
}

void loop() {
}

◆ highByte

#define highByte (   w)    ((uint8_t) ((w) >> 8))
Description
Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type)
Syntax
highByte(w);
Parameters
wa value of any type
Returns
byte
Example
void setup() {
  Serial.begin(115200);
  Serial.println("Test target 0xABCD");

  Serial.print("lowByte() of 0xABCD = "); // extract the low-order byte of the target
  Serial.println(lowByte(target), HEX);

  Serial.print("highByte() of 0xABCD = "); // extract the low-order byte of the target
  Serial.println(highByte(target), HEX);
}

void loop() {
}