Created
July 12, 2024 19:41
-
-
Save fzn0x/a0df538337d9974a3c00141563b05cdb to your computer and use it in GitHub Desktop.
Bit Flip - Bitnya ngeflip
This file contains hidden or 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 bitFlip(byte, bitPosition) { | |
// Membuat mask untuk bit flip | |
const mask = 1 << bitPosition; | |
return byte ^ mask; | |
} | |
function byteToBinaryString(byte) { | |
return byte.toString(2).padStart(8, "0"); | |
} | |
const originalByte = 0b10101010; | |
console.log("Byte asli : ", byteToBinaryString(originalByte)); | |
const bitPosition = 3; | |
const flippedByte = bitFlip(originalByte, bitPosition); | |
console.log( | |
"Byte setelah flip di posisi", | |
bitPosition, | |
": ", | |
byteToBinaryString(flippedByte) | |
); | |
console.log("Nilai byte asli : ", originalByte); | |
console.log("Nilai byte setelah flip: ", flippedByte); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment