Skip to content

Instantly share code, notes, and snippets.

@markcode
Last active December 6, 2015 23:09
Show Gist options
  • Save markcode/a59af749497870fbce3e to your computer and use it in GitHub Desktop.
Save markcode/a59af749497870fbce3e to your computer and use it in GitHub Desktop.
Node.js - Convert Byte (octet) to Bits Funcation
function Byte2Bits(octet) {
var bits = [];
var i = 0;
for ( i = 0; i < 8; i++ ) {
bits.push(octet >> 7 - i & 1);
}
return bits;
}
// example:
var octet = new Buffer('Z');
// <Buffer 5a>
var bits = Byte2Bits(octet[0]);
console.log(bits);
// [ 0, 1, 0, 1, 1, 0, 1, 0 ]
@markcode
Copy link
Author

markcode commented Dec 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment