Skip to content

Instantly share code, notes, and snippets.

@itsthatguy
Last active August 29, 2015 13:56
Show Gist options
  • Save itsthatguy/9166257 to your computer and use it in GitHub Desktop.
Save itsthatguy/9166257 to your computer and use it in GitHub Desktop.
A few functions to help you test your bitwise operations
Foo = require("./foo.coffee")
describe 'Foo', ->
testOpts =
initialNumber: 255
initialArray: [255,255,255]
expectedBinary: 10010010101110101000
it 'should return a correct binary string', ->
foo = new Foo()
binary = foo.intToBinary(testOpts.initialNumber)
binary.should.equal "11111111"
it 'should return a correct hexadecimal string', ->
foo = new Foo()
binary = foo.intToHexadecimal(testOpts.initialNumber)
binary.should.equal "ff"
it 'should return a correct binary string from an array', ->
foo = new Foo()
binary = foo.arrayToBinary(testOpts.initialArray)
binary.should.equal "111111111111111111111111"
it 'should return a correct hexadecimal string from an array', ->
foo = new Foo()
binary = foo.arrayToHexadecimal(testOpts.initialArray)
binary.should.equal "ffffff"
class Foo
arrayToBinary: (array) -> (array.map (n) => @intToBinary(n)).join('')
arrayToHexadecimal: (array) -> (array.map (n) => @intToHexadecimal(n)).join('')
intToHexadecimal: (number) -> return parseInt(number).toString(16)
intToBinary: (number) -> return parseInt(number).toString(2)
module.exports = Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment