Skip to content

[RFC]: add bit manipulation utilities #3210

Open
@RaviSadam

Description

@RaviSadam

Description

Description

This RFC proposes the introduction of Bit Manipulation Utilities to the stdlib.js library.
Bit manipulation is a common requirement in low-level programming, performance-critical tasks, and algorithms that require efficient data processing (e.g., cryptography, compression, and game development). Adding these utilities to stdlib.js would make the library more versatile and useful for developers who rely on optimized bitwise operations in JavaScript.

Proposed Utilities:

setBit(number, position) – Set the bit at a specific position to 1.
clearBit(number, position) – Clear the bit at a specific position to 0.
toggleBit(number, position) – Toggle the bit at a specific position (flip 0 to 1 or 1 to 0).
checkBit(number, position) – Check if the bit at a specific position is 1 or 0.
countSetBits(number) – Count the number of 1s in the binary representation of a number (Hamming weight).
isPowerOfTwo(number) – Check if a number is a power of two.
getLowestSetBit(number) – Get the position of the lowest set bit.
reverseBits(number) – Reverse the bits in a number.
find_lowest_set_bit(num): Returns the position of the least significant bit set to 1 in num.
find_highest_set_bit(num): Returns the position of the most significant bit set to 1 in num.
rightmost_set_bit_mask(num): Returns a mask with only the rightmost set bit of num.
bit_mask(n): Returns a bitmask with the first n bits set to 1.
swap_bits(num,pos1, pos2): Swaps the bits at positions pos1 and pos2 in num.
**rotate_bits_left(num,shift): **Performs a left circular shift by shift positions in num.
rotate_bits_right(num, shift): Performs a right circular shift by shift positions in num.
**parity_check(num): Checks if the number of 1 bits in num is odd or even.
bitwise_and(num1,num2): Computes the bitwise AND of num1 and num2.
bitwise_or(num1, num2): Computes the bitwise OR of num1 and num2.
bitwise_xor(num1,num2): Computes the bitwise XOR of num1 and num2.
bitwise_not(num): Computes the bitwise NOT of num (inverts all bits).
left_shift(num, shift): Shifts the bits of num to the left by shift positions.
right_shift(num,shift): Shifts the bits of num to the right by shift positions.
modulo_power_of_two(num,n): Computes num % 2^n using bitwise operations.

I would like to work on this issue. I am happy to collaborate, address feedback, and ensure the utilities meet the quality and performance standards of stdlib.js.

Related Issues

Related issues # , # , and # .

Questions

No.

Other

No.

Checklist

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureIssue or pull request for adding a new feature.Needs DiscussionNeeds further discussion.RFCRequest for comments. Feature requests and proposed changes.UtilitiesIssue or pull request concerning general utilities.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions