Skip to content

Commit

Permalink
first and last
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Feb 9, 2023
1 parent 0b5f805 commit c437170
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ const result = bitset
.toggleBE(1) // 0100 0010
.unsign() // >>> 0
.value

const last6 = bitset
.last(6) // 00 0010
.toString() // "000010"
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@hazae41/bitset",
"version": "1.0.0",
"version": "1.0.1",
"description": "Utilities for arithmetic bitwise operations in JavaScript",
"homepage": "https://github.com/hazae41/bitset",
"repository": "github:hazae41/bitset",
Expand Down
8 changes: 4 additions & 4 deletions src/mods/bitset/bitset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ test("Export Int32 to Uint32", async () => {
test("First", async () => {
const bitset = new Bitset(0b11100011, 8)

assert(bitset.first(2) === 3)
assert(bitset.first(3) === 7)
assert(bitset.first(2).value === 3)
assert(bitset.first(3).value === 7)
})

test("Last", async () => {
const bitset = new Bitset(0b11100111, 8)

assert(bitset.last(2) === 3)
assert(bitset.last(3) === 7)
assert(bitset.last(2).value === 3)
assert(bitset.last(3).value === 7)
})
39 changes: 21 additions & 18 deletions src/mods/bitset/bitset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Bitset {
/**
* Get the value as a left-padded binary string
*
* @returns
* @returns string
*/
toString() {
return this.value.toString(2).padStart(this.length, "0")
Expand All @@ -17,7 +17,7 @@ export class Bitset {
/**
* Transform the value to an unsigned 32-bits number
*
* @returns
* @returns the same Bitset
*/
unsign() {
this.value = this.value >>> 0
Expand All @@ -28,7 +28,7 @@ export class Bitset {
/**
* Bitwise NOT
*
* @returns
* @returns the same Bitset
*/
not() {
for (let i = 0; i < this.length; i++)
Expand All @@ -40,18 +40,17 @@ export class Bitset {
* Get the bit at big-endian index
*
* @param index
* @returns
* @returns boolean
*/
getBE(index: number) {
return Boolean(this.value & (1 << (this.length - index - 1)))
}


/**
* Get the bit at little-endian index
*
* @param index
* @returns
* @returns boolean
*/
getLE(index: number) {
return Boolean(this.value & (1 << index))
Expand All @@ -61,7 +60,7 @@ export class Bitset {
* Toggle the bit at big-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
toggleBE(index: number) {
this.value ^= (1 << (this.length - index - 1))
Expand All @@ -73,7 +72,7 @@ export class Bitset {
* Toggle the bit at little-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
toggleLE(index: number) {
this.value ^= (1 << index)
Expand All @@ -85,7 +84,7 @@ export class Bitset {
* Enable the bit at big-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
enableBE(index: number) {
this.value |= (1 << (this.length - index - 1))
Expand All @@ -97,7 +96,7 @@ export class Bitset {
* Enable the bit at little-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
enableLE(index: number) {
this.value |= (1 << index)
Expand All @@ -109,7 +108,7 @@ export class Bitset {
* Disable the bit at big-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
disableBE(index: number) {
this.value &= ~(1 << (this.length - index - 1))
Expand All @@ -121,7 +120,7 @@ export class Bitset {
* Disable the bit at little-endian index
*
* @param index
* @returns
* @returns the same Bitset
*/
disableLE(index: number) {
this.value &= ~(1 << index)
Expand All @@ -134,7 +133,7 @@ export class Bitset {
*
* @param index
* @param value
* @returns
* @returns the same Bitset
*/
setBE(index: number, value: boolean) {
if (value)
Expand All @@ -148,7 +147,7 @@ export class Bitset {
*
* @param index
* @param value
* @returns
* @returns the same Bitset
*/
setLE(index: number, value: boolean) {
if (value)
Expand All @@ -161,19 +160,23 @@ export class Bitset {
* Get first count bits
*
* @param count number of bits to get
* @returns
* @returns a new Bitset
*/
first(count: number) {
return this.value >> (this.length - count)
const value = this.value >> (this.length - count)

return new Bitset(value, count)
}

/**
* Get last count bits
*
* @param count number of bits to get
* @returns
* @returns a new Bitset
*/
last(count: number) {
return this.value & ((1 << count) - 1)
const value = this.value & ((1 << count) - 1)

return new Bitset(value, count)
}
}

0 comments on commit c437170

Please sign in to comment.