1
0
jcktrue.dk/content/post/bit-magic/index.md
Jens True 6f8f5bfe06
All checks were successful
continuous-integration/drone/push Build is passing
Theme update. Work on bitmagic post
2020-07-05 11:14:47 +02:00

1.4 KiB

title subtitle summary authors tags categories date lastmod featured draft image projects
Bit magic in C (and C++)
2020-06-13T19:01:19+02:00 2020-06-13T19:01:19+02:00 false true
caption focal_point preview_only
false

Set a bit

// Set one bit high at position "bit" in input and store to output
output = input | (1 << bit);
// Shorthand
variable |= (1 << bit);
// Multiple bits
variable |= (0b101 << bit);

Clear a bit

// Clear one bit high at position "bit" in input and store to output
output = input & ~(1 << bit);
// Shorthand
variable &= ~(1 << bit);
// Multiple bits
variable &= ~(0b101 << bit);

Toggle a bit

// Clear one bit high at position "bit" in input and store to output
output = x ^ (1 << bit);
// Shorthand
variable ^= (1 << bit);
// Multiple bits
variable ^= (0b101 << bit);