Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Created July 21, 2021 04:19
Show Gist options
  • Save jstaursky/278cde13a1cceace28b3009749cc4084 to your computer and use it in GitHub Desktop.
Save jstaursky/278cde13a1cceace28b3009749cc4084 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cstdint>
#include <fmt/format.h>
using namespace std;
int main(int argc, char *argv[])
{
uint8_t a = 0b1111'0000;
auto b = vector<uint8_t> {a,a,a,a,a,a,a,a,a};
auto c = b;
auto d = c;
fmt::print (" {:b}\n\n", a);
// bit clearing
for (int i = 0; i != 8; ++i) {
b[i] &= ~(1 << i);
fmt::print("{} {:8b} \n", i , b[i]);
}
fmt::print("\n");
// bit flipping
for (int i = 0; i != 8; ++i) {
c[i] ^= (1 << i);
fmt::print("{} {:8b} \n", i , c[i]);
}
fmt::print("\n");
// bit setting
for (int i = 0; i != 8; ++i) {
d[i] |= (1 << i);
fmt::print("{} {:8b} \n", i , d[i]);
}
return 0;
}
// g++ -std=c++17 test.cpp -lfmt -o test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment