Skip to content

Commit

Permalink
fix(common): update bitmask implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcincichocki authored Oct 8, 2021
1 parent ac8f6a0 commit 4f1206d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 20 additions & 16 deletions src/common/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,32 @@ describe('utils', () => {
});

it('should correctly recognize flags in bit mask', () => {
const mask = new BitMask(4); // 100
const mask = new BitMask(1); // 001

// M 100
// F 000
expect(mask.has(0)).toBe(false);
// M 001
expect(mask.has(1)).toBe(true);
expect(mask.has(2)).toBe(false);
expect(mask.has(4)).toBe(false);

mask.add(1);
// M 011
mask.add(2);

// M 101
// F 111
expect(mask.has(7)).toBe(true);
expect(mask.has(1)).toBe(true);
expect(mask.has(2)).toBe(true);
expect(mask.has(4)).toBe(false);

mask.delete(3);
// M 111
mask.add(4);

// M 010
// F 000
expect(mask.has(0)).toBe(false);
expect(mask.has(1)).toBe(true);
expect(mask.has(2)).toBe(true);
expect(mask.has(4)).toBe(true);

const m2 = new BitMask(0);
// M 011
mask.delete(4);

// M 000
// F 001
expect(m2.has(1)).toBe(false);
expect(mask.has(1)).toBe(true);
expect(mask.has(2)).toBe(true);
expect(mask.has(4)).toBe(false);
});
});
4 changes: 2 additions & 2 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export class BitMask {
constructor(public mask: number) {}

has(flag: number) {
return (this.mask & flag) !== 0;
return (this.mask & flag) === flag;
}

add(flag: number) {
this.mask = this.mask | flag;
this.mask |= flag;

return this;
}
Expand Down

0 comments on commit 4f1206d

Please sign in to comment.