From ef0ce890afa1517011859ebe8c240178fdd154ed Mon Sep 17 00:00:00 2001 From: JBatzill Date: Tue, 25 Nov 2014 17:04:02 +0100 Subject: Create bitOps.cpp --- sonstiges/bitOps.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 sonstiges/bitOps.cpp diff --git a/sonstiges/bitOps.cpp b/sonstiges/bitOps.cpp new file mode 100644 index 0000000..df8cd8f --- /dev/null +++ b/sonstiges/bitOps.cpp @@ -0,0 +1,22 @@ +[lsb: 0-th bit, msb: n-th bit,] + +Get j-th bit: +(a & (1 << j)) != 0 + +Set j-th bit: +a |= (1 << j) + +Clear j-th bit: +a &= ~(1 << j) + +Toggle j-th bit: +a ^= (1 << j) + +Get value of first set bit: +(a & -a) + +Turn on all bits: +a = -1 + +Turn on first n bits: +a = (1 << n) - 1 -- cgit v1.2.3