Skip to content

Instantly share code, notes, and snippets.

@bosley
Last active April 2, 2020 04:01
Show Gist options
  • Save bosley/8f6363cec71e24bf0fb4f1afdc202eef to your computer and use it in GitHub Desktop.
Save bosley/8f6363cec71e24bf0fb4f1afdc202eef to your computer and use it in GitHub Desktop.
#include <bitset>
#include <iostream>
void showXorOp() {
uint8_t var = 0xFF; // 255
uint8_t var1 = 0X0F; // 15
std::cout << (int)var << "\t" << std::bitset<8>(var) << std::endl;
std::cout << (int)var1 << "\t" << std::bitset<8>(var1) << std::endl;
std::cout << "result" << "\t" << std::bitset<8>(var ^ var1) << std::endl;
}
void showAndOp() {
uint8_t var = 0x05; // 5
uint8_t var1 = 0x03; // 3
std::cout << (int)var << "\t" << std::bitset<8>(var) << std::endl;
std::cout << (int)var1 << "\t" << std::bitset<8>(var1) << std::endl;
std::cout << "result" << "\t" << std::bitset<8>(var & var1) << std::endl;
}
void showOrOp() {
uint8_t var = 0x48; // 72
uint8_t var1 = 0xB8; // 184
std::cout << (int)var << "\t" << std::bitset<8>(var) << std::endl;
std::cout << (int)var1 << "\t" << std::bitset<8>(var1) << std::endl;
std::cout << "result" << "\t" << std::bitset<8>(var | var1) << std::endl;
}
void showNegOp() {
uint8_t var = 0xFA; // 250
std::cout << (int)var << "\t" << std::bitset<8>(var) << std::endl;
std::cout << "result" << "\t" << std::bitset<8>(~var) << std::endl;
}
void showMask() {
uint32_t alphaMask = 0xFF000000; // Mask to obtain alpha byte
uint32_t blueMask = 0x00FF0000; // Mask to obtain blue byte
uint32_t greenmask = 0x0000FF00; // Mask to obtain green byte
uint32_t redMask = 0x000000FF; // Mask to obtain red byte
std::cout << "alphaMask" << "\t" << std::bitset<32>(alphaMask) << std::endl; // Show the mask
std::cout << "blueMask" << "\t" << std::bitset<32>(blueMask ) << std::endl; // Show the mask
std::cout << "greenmask" << "\t" << std::bitset<32>(greenmask) << std::endl; // Show the mask
std::cout << "redMask " << "\t" << std::bitset<32>(redMask ) << std::endl; // Show the mask
// A B G R
uint32_t abgrColor = 0x8CE90A03; // The actual 32-bit color
std::cout << "32bit Color" << "\t"
<< std::bitset<8>(0x8C) << "|" // Alpha color
<< std::bitset<8>(0xE9) << "|" // Blue color
<< std::bitset<8>(0x0A) << "|" // Green color
<< std::bitset<8>(0x03) << std::endl; // Red color
std::cout << "Alpha (Byte 1)" << "\t" << std::bitset<32>(abgrColor & alphaMask) << std::endl;
std::cout << "Blue (Byte 2)" << "\t" << std::bitset<32>(abgrColor & blueMask ) << std::endl;
std::cout << "Green (Byte 3)" << "\t" << std::bitset<32>(abgrColor & greenmask) << std::endl;
std::cout << "Red (Byte 4)" << "\t" << std::bitset<32>(abgrColor & redMask ) << std::endl;
}
void showShifting() {
uint8_t var = 0x01;
std::cout << "0x01 = " << std::bitset<8>(var) << std::endl;
std::cout << "0x01 << 1 = " << std::bitset<8>(var << 1 ) << std::endl;
std::cout << "0x01 << 2 = " << std::bitset<8>(var << 2 ) << std::endl;
std::cout << "0x01 << 3 = " << std::bitset<8>(var << 3 ) << std::endl;
std::cout << "Shifting works the same way in the other direction" << std::endl;
std::cout << "Check if a bit 8 is set!" << std::endl;
var = 0x8A;
std::cout << "Var = " << std::bitset<8>(var) << std::endl;
std::cout << "Var >> 7 = " << std::bitset<8>(var >> 7) << std::endl;
bool isBitSet = var >> 7;
if(isBitSet) {
std::cout << "The bit was set!" << std::endl;
} else {
std::cout << "Oh no!" << std::endl;
}
}
void showChopping() {
uint32_t var = 0x8CE90A03;
std::cout << "Var:\t\t" << std::bitset<32>(var) << std::endl;
uint8_t pack[4];
pack[0] = ( var & 0x000000FF);
pack[1] = ( var & 0x0000FF00) >> 8;
pack[2] = ( var & 0x00FF0000) >> 16;
pack[3] = ( var & 0xFF000000) >> 24;
std::cout << "Chopped bytes:\t" << std::bitset<8>(pack[0]) << " | "
<< std::bitset<8>(pack[1]) << " | "
<< std::bitset<8>(pack[2]) << " | "
<< std::bitset<8>(pack[3]) << std::endl;
uint32_t unpacked = pack[0] | (pack[1] << 8) | (pack[2] << 16) | (pack[3] << 24);
std::cout << "Un-Chopped :\t" << std::bitset<32>(var) << std::endl;
if(unpacked == var) {
std::cout << "Success!" << std::endl;
}
}
int main() {
std::cout << " --- AND --- " << std::endl;
showAndOp();
std::cout << " --- OR --- " << std::endl;
showOrOp();
std::cout << " --- XOR --- " << std::endl;
showXorOp();
std::cout << " --- NEG --- " << std::endl;
showNegOp();
std::cout << " --- MASK --- " << std::endl;
showMask();
std::cout << " --- SHIFT --- " << std::endl;
showShifting();
std::cout << " --- CHOP --- " << std::endl;
showChopping();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment