1
0
jcktrue.dk/content/post/bit-magic/index.md
Jens True 25afb5e4e0
All checks were successful
continuous-integration/drone/push Build is passing
First step for a bit manipulation page
2020-06-25 22:43:14 +02:00

1.3 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);

Clear a bit

// Clear one bit high at position "bit" in input and store to output
output = input & ~(1 << bit);
// Shorthand
variable &= ~(1 << 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);