Created
June 28, 2017 05:52
-
-
Save ParthBarot-BoTreeConsulting/8e9555cf0e26485738d5c5d1d9c85cd9 to your computer and use it in GitHub Desktop.
JS - 32 bit integer to bytes
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
~$ node | |
> var calculatedChecksum = 1513135 | |
// Anding an integer with 0xFF leaves only the least significant byte. | |
// For example, to get the first byte, we can write "<int value> & 0xFF" | |
// This is typically referred to as "masking" | |
> var firstByte = calculatedChecksum & 0XFF | |
// 0XFF00 is hexadecimal, We are masking out the lower byte of your number, | |
// then bit-shifting all of the bits to the right by 8. | |
// Essentially, we are grabbing the most significant byte of your number and seeing what that is. | |
> var secondByte = (calculatedChecksum & 0XFF00) >> 8 // instead of ">>8" we can do "/256" | |
> firstByte | |
175 | |
> secondByte | |
22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment