Pārlūkot izejas kodu

Added a small utility for bit manipulation. Tests included.

master
Bryan Miller pirms 5 gadiem
vecāks
revīzija
6e68f6e8d2
3 mainītis faili ar 55 papildinājumiem un 1 dzēšanām
  1. +1
    -1
      package.json
  2. +23
    -0
      src/utils/bitman.js
  3. +31
    -0
      test/utils/bitman.spec.js

+ 1
- 1
package.json Parādīt failu

@@ -4,7 +4,7 @@
"description": "Fantasy 8Bit (F8) chip emulator",
"main": "f8.js",
"scripts": {
"test": "mocha"
"test": "mocha -R spec --recursive"
},
"keywords": [
"fantasy",

+ 23
- 0
src/utils/bitman.js Parādīt failu

@@ -0,0 +1,23 @@

var bitman = {};

bitman.isOn = function(num, bit){
return ((num>>bit) % 2) !== 0;
}

bitman.set = function(num, bit){
return num | (1<<bit);
}

bitman.clear = function(num, bit){
return num & ~(1<<bit);
}

bitman.toggle = function(num, bit){
return (bitman.isOn(num, bit)) ?
bitman.clear(num, bit) :
bitman.set(num, bit);
}


module.exports = bitman;

+ 31
- 0
test/utils/bitman.spec.js Parādīt failu

@@ -0,0 +1,31 @@
const BIT = require('../../src/utils/bitman.js');
const expect = require('chai').expect;

describe("Testing utils/bitman ...", function(){
it(".isOn() 'true' test", function(){
expect(BIT.isOn(parseInt("0010", 2), 1)).be.true;
});

it(".isOn() 'false' test", function(){
expect(BIT.isOn(parseInt('0111', 2), 3)).be.false;
});

it(".set()", function(){
let num = 1;
num = BIT.set(num, 1);
expect(num).to.equal(3);
});

it(".clear()", function(){
let num = 15;
num = BIT.clear(num, 2);
expect(num).to.equal(11);
});

it(".toggle()", function(){
let num = 9;
num = BIT.toggle(num, 3);
num = BIT.toggle(num, 2);
expect(num).to.equal(5);
});
});

Notiek ielāde…
Atcelt
Saglabāt