47 lines
No EOL
1.4 KiB
C
47 lines
No EOL
1.4 KiB
C
//
|
|
// Created by Kyle Stevenson on 01/02/2025.
|
|
//
|
|
|
|
#include "bit_operations.h"
|
|
|
|
void setBit(uint8_t *numberArray, const int bitIndex) {
|
|
// Determine the byte index where the bit is stored
|
|
const int byteIndex = bitIndex / 8;
|
|
|
|
// Determine the bit position within the byte
|
|
const int bitPosition = bitIndex % 8;
|
|
|
|
// Create a bitmask where only the target bit is set
|
|
const uint8_t bitmask = 1 << bitPosition;
|
|
|
|
// Use bitwise OR to set the specific bit without affecting others
|
|
numberArray[byteIndex] |= bitmask;
|
|
}
|
|
|
|
void clearBit(uint8_t *numberArray, const int bitIndex) {
|
|
// Determine the byte in which the bit is stored
|
|
const int byteIndex = bitIndex / 8;
|
|
|
|
// Determine the bit position within the byte
|
|
const int bitPosition = bitIndex % 8;
|
|
|
|
// Create a bitmask where only the target bit is 1
|
|
const uint8_t bitmask = 1 << bitPosition;
|
|
|
|
// Use bitwise AND with bitwise NOT to clear the specific bit
|
|
numberArray[byteIndex] &= ~bitmask;
|
|
}
|
|
|
|
bool checkBit(const uint8_t *numberArray, const int bitIndex) {
|
|
// Determine the byte containing the bit
|
|
const int byteIndex = bitIndex / 8;
|
|
|
|
// Determine the bit position within the byte
|
|
const int bitPosition = bitIndex % 8;
|
|
|
|
// Create a bitmask with only the target bit set
|
|
const uint8_t bitmask = 1 << bitPosition;
|
|
|
|
// Use bitwise AND to check if the bit is set
|
|
return (numberArray[byteIndex] & bitmask) != 0;
|
|
} |