Last active
February 5, 2020 07:32
-
-
Save SET001/399269bb66412916e09dece9fb411f63 to your computer and use it in GitHub Desktop.
truncate number to size in 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
const bitwiseTruncator = length => number => number & (Math.pow(2, length)-1); | |
((length, cTestNumbers, cTestNumbersMaxSize=999999)=>{ | |
const numbers = Array.from({length: cTestNumbers}).map(()=>Math.ceil(Math.random()*cTestNumbersMaxSize)) | |
const truncatedNumbers = numbers.map(bitwiseTruncator(length)) | |
numbers.map((number)=>console.log(`was: ${number.toString(2)}`)) | |
truncatedNumbers.map((number)=>console.log(`now: ${number.toString(length).padStart(length, "0")}`)) | |
})(3, 7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment