Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integer.addBit, integer.removeBit and improve integer.setBit, integer.toggleBit, integer.getBit #59

Open
5 tasks
aandrejevas opened this issue Aug 21, 2024 · 0 comments
Assignees

Comments

@aandrejevas
Copy link

aandrejevas commented Aug 21, 2024

Hello, integer.addBit and integer.removeBit methods should be added to the library.

An implementation could look like this:

  • add-bit.ts
export default (int32: number, position: number): number => int32 | (1 << position)
  • remove-bit.ts
export default (int32: number, position: number): number => int32 & ~(1 << position)

Also integer.setBit, integer.toggleBit and integer.getBit functions could be improved:

  • set-bit.ts
// Current.
export default (int32: number, position: number, value: Bit): Bit =>
	<Bit>(value === 1 ? int32 | (1 << position) : int32 & ~(1 << position))

// Proposed. The return type should be number, not Bit.
export default (int32: number, position: number, value: Bit): number =>
	(value === 1 ? int32 | (1 << position) : int32 & ~(1 << position))

// Proposed. Would be nice if we could pass a boolean to setBit.
export default (int32: number, position: number, value: Bit | boolean): number =>
	(!!value ? int32 | (1 << position) : int32 & ~(1 << position))
  • toggle-bit.ts
// Current.
export default (int32: number, position: number) => int32 ^ (1 << position)

// Proposed. The return type should be specified.
export default (int32: number, position: number): number => int32 ^ (1 << position)
  • get-bit.ts
// Current.
export default (int32: number, position: number): Bit =>
	<Bit>((int32 >> position) & 1)

// Proposed. Unsigned right shift should be used.
export default (int32: number, position: number): Bit =>
	<Bit>((int32 >>> position) & 1)
@aandrejevas aandrejevas changed the title Add integer.addBit and integer.removeBit Add integer.addBit, integer.removeBit and improve integer.setBit, integer.toggleBit, integer.getBit Aug 22, 2024
@FlorianWendelborn FlorianWendelborn self-assigned this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants