Skip to content

Commit

Permalink
Introduction to bitwise with cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Feb 13, 2024
1 parent 869dc83 commit 137d879
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions theory/bitwise.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <iostream>
#include <bitset>

int main(void) {

// Printing integers in Binary
unsigned short int data {0b11111110}; // 254
unsigned short int a{0b01000100}; // testing negation
unsigned short int b = ~a;

std::cout << "Section 1: Printing integers in binary or other" << std::endl;

std::cout << "a value: " << std::bitset<8>(a) << std::endl;
std::cout << "~a(b) value: " << std::bitset<8>(b) << std::endl;

std::cout << "data (dec): " << std::dec << data << std::endl;
std::cout << "data (hex): " << std::hex << std::showbase << data << std::endl;
std::cout << "data (bin): " << std::bitset<8>(data) << std::endl;

std::cout << std::endl;

// Shift operators (just fot int and char)

/*
<< shift to the left (same as to multiply vy 2)
>> shift to the right (same as to divide by 2)
obs: you can't get back a data that was lost by shifting using
the inverse operation
*/

unsigned short int value {0b00001000};

std::cout << "Section 2: Shifting Operations" << std::endl;

// Printing value before left shift
std::cout << "Value (bin): " << std::bitset<8>(value) << std::endl;
std::cout << "Value (dec): " << std::dec << value << std::endl;

// Doing explicit cast because CPU will recognize like a int
value = static_cast<unsigned short int>(value << 1); // just change to see what happen if shift to right

std::cout << "Value (bin): " << std::bitset<8>(value) << std::endl;
std::cout << "Value (dec): " << std::dec << value << std::endl;

std::cout << std::endl;

// Logical Bitwise operators

/*
& (AND)
| (OR)
^ (XOR)
~ (NOT)
*/

// Assigments Operators

/*
>>=
<<=
|=
&=
^=
*/

unsigned short int compare {0b00011111};
unsigned short int result;

std::cout << "Section 3: Logical and assigments operators" << std::endl;

// &= compare
result = static_cast<unsigned short int>(value &= compare);
std::cout << "Value (bin): " << std::bitset<8>(result) << std::endl;
std::cout << "Value (dec): " << std::dec << result << std::endl;

// |= compare
result = static_cast<unsigned short int>(value |= compare);
std::cout << "Value (bin): " << std::bitset<8>(result) << std::endl;
std::cout << "Value (dec): " << std::dec << result << std::endl;

// ^= compare
result = static_cast<unsigned short int>(value ^= compare);
std::cout << "Value (bin): " << std::bitset<8>(result) << std::endl;
std::cout << "Value (dec): " << std::dec << result << std::endl;

std::cout << std::endl;

// Masks

/*
Highlight position for bit of interest with a 1, mask other positions
with 0
*/

}












0 comments on commit 137d879

Please sign in to comment.