Socket
Socket
Sign inDemoInstall

bit-macros

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    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.


Version published
Maintainers
1
Created

Readme

Source

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); // 2
console.log(integer_tuesday): // 4

const monday_and_tuesday = integer_monday + integer_tuesday;

console.log(monday_and_tuesday); // 6

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;

// The bit count starts from 0, so 1 means the second bit in the sequence
if (tst_bit(value, 1)) {
	// Should be truthy since 2 in bit sequence is 00000010
	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); // 254;

cpl_bit

const { cpl_bit } = require("bit-macros");

const value = 255;

const coupled_value = cpl_bit(value, 7);

console.log(coupled_value); // 127

const coupled_value2 = cpl_bit(coupled_value, 7);

console.log(coupled_value2); // 255

TODO

  • Add set/tst/clr/cpl many bits

FAQs

Last updated on 12 Oct 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc