start.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3100/spring2026/notes/binary/code/start.cpp
 
#include <iostream>
#include <iomanip>
#include <format>

using namespace std;

int main() {
    //     1 0 1 0 1 1 1 0
    //     1 0 1 0    1 1 1 0 
    //        A          E
    //     1 0    1 0 1   1 1 0 
    //      2       5       6
    int number{128+32+8+4+2};

    cout << "The number is " << number << endl;
    cout << hex << "The number in hex is " << number << endl;
    cout << oct << "The number in octal is " << number << endl;
    cout << dec;

    cout << "The number in binary is " << format("{:b}",number) << endl; 


    /*cout << "The size of a char is " << sizeof(char) << endl;
    cout << "The size of a short is " << sizeof(bit) << endl;
    cout << "The size of a int is " << sizeof(int) << endl;
    cout << "The size of a long is " << sizeof(long) << endl;
    cout << "The size of a long long is " << sizeof(long long) << endl;
    */

    unsigned short bit{1};
    cout << endl << endl;
    int count = 0;

    while (count < sizeof(bit)*8){ 
        cout << setw(10)  << count << setw(20) << bit 
             << setw(sizeof(bit) * 8  + 1)<< format("{:b}",bit)<< endl;
        bit = bit << 1;
        ++count;
    }
    cout << setw(10)  << count << setw(20) << bit << endl;

    cout << endl << endl;

    number = 42 << 2;
    cout << " 42 << 2 = "  << number << endl;


    // what is &&    number && bit    logical and
    // what is &     number & bit     bitwise and
    //
   
    unsigned short num1 = 128+32+8+4+2;
    bit = 1;
    count = 0;
    while (count < sizeof(bit) * 8  - 1) {
       int digit = bit & num1;
       cout << digit << endl;
       bit  = bit << 1; 
       ++count;
    }
    cout << endl;

    return 0;
}