Last active
December 6, 2015 23:09
-
-
Save markcode/a59af749497870fbce3e to your computer and use it in GitHub Desktop.
Node.js - Convert Byte (octet) to Bits Funcation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Byte2Bits(octet) { | |
var bits = []; | |
var i = 0; | |
for ( i = 0; i < 8; i++ ) { | |
bits.push(octet >> 7 - i & 1); | |
} | |
return bits; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example: | |
var octet = new Buffer('Z'); | |
// <Buffer 5a> | |
var bits = Byte2Bits(octet[0]); | |
console.log(bits); | |
// [ 0, 1, 0, 1, 1, 0, 1, 0 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see: http://stackoverflow.com/questions/18088625/node-js-slice-a-byte-into-bits