bit-macros
This is a set of C macros used in embedded systems development for bit level operations. You can use them to directly manipulate the bit values on a integer variable.
It contains 4 fundamental macros:
- set_bit: used to set the value of a bit from 0 to 1;
- tst_bit: used to check if a bit inside a set of bits is equal to 1;
- clr_bit: used to set the value of a bit from 1 to 0;
- cpl_bit: used to toggle the bit value (from 0 to 1 or from 1 to 0);
set_bit
const { set_bit } = require("bit-macros");
const integer_monday = set_bit(0, 1);
const integer_tuesday = set_bit(0, 2);
console.log(integer_monday);
console.log(integer_tuesday):
const monday_and_tuesday = integer_monday + integer_tuesday;
console.log(monday_and_tuesday);
tst_bit
This macro works with falsy values. It returns an integer different of zero if the bit is actually activated on the value passed as argument.
const { tst_bit } = require("bit-macros");
const value = 2;
if (tst_bit(value, 1)) {
console.log("The variable has the second bit enabled");
} else {
console.log("The variable doesn't have the second bit enabled");
}
clr_bit
const { clr_bit } = require("bit-macros");
const value = 255;
const cleared_value = clr_bit(value, 0);
console.log(cleared_value);
cpl_bit
const { cpl_bit } = require("bit-macros");
const value = 255;
const coupled_value = cpl_bit(value, 7);
console.log(coupled_value);
const coupled_value2 = cpl_bit(coupled_value, 7);
console.log(coupled_value2);
TODO