Skip to main content

Bits and Bytes

Bits and Bytes

This library is for extracting the byte of a variable and accessing a bit of a number etc.

lowByte

Description

Extracts the low-order (rightmost) byte of a variable.

Syntax

lowByte(x)

Parameters

x: A value of any type

Returns

Byte (unsigned char).

highByte

Description

Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type).

Syntax

highByte(x)

Parameters

x: A value of any type

Returns

Byte (unsigned char).

bitRead

Description

Reads a bit of a number.

Syntax

bitRead(x, n)

Parameters

x: The number from which to read
n: Which bit to read, starting at 0 for the least-significant (rightmost) bit

Returns

The value of the bit (0 or 1).

bitWrite

Description

Writes a bit of a numeric variable.

Syntax

bitWrite(x, n, b)

Parameters

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)

Returns

None

bitSet

Description

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

Syntax

bitSet(x, n)

Parameters

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

Returns

None

bitClear

Description

Clears (writes a 0 to) a bit of a numeric variable.

Syntax

bitClear(x, n)

Parameters

x: The numeric variable whose bit to clear
n: Which bit to clear, starting at 0 for the least-significant (rightmost) bit

Returns

None

bit

Description

Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.).

Syntax

bit(n)

Parameters

n: The bit whose value to compute

Returns

The value of the bit.


Example

Byte control.


        #include <Arduino.h>
        void setup(){
          Serial.begin(9600);
           
          uint16_t original_data = 0b1111000010100101;
           
          Serial.println("BIN");
          Serial.print("original:\t");
          Serial.println(original_data, BIN);
          Serial.print("lowByte:\t");
          Serial.println(lowByte(original_data), BIN);
          Serial.print("highByte:\t");
          Serial.println(highByte(original_data), BIN);
           
          Serial.println("HEX");
          Serial.print("original:\t");
          Serial.println(original_data, HEX);
          Serial.print("lowByte:\t");
          Serial.println(lowByte(original_data), HEX);
          Serial.print("highByte:\t");
          Serial.println(highByte(original_data), HEX);
         
        }
         
        void loop(){
        }