Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Created July 12, 2024 19:41
Show Gist options
  • Save fzn0x/a0df538337d9974a3c00141563b05cdb to your computer and use it in GitHub Desktop.
Save fzn0x/a0df538337d9974a3c00141563b05cdb to your computer and use it in GitHub Desktop.
Bit Flip - Bitnya ngeflip
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