Show Bits with C++   Leave a comment

// Show Bits with C++
//
// Created by ED on 2013-04-13

#include <iostream>
using namespace std;

class Bits {
    unsigned int val;
public:
    bool formatted = false;
    
    Bits(unsigned int val) {
        this->val = val;
    }
    
    void print() {
        unsigned int m = 1 << sizeof(val) * 8 - 1;
        unsigned int c = 1;
        while (m) {
            cout << (val & m ? '1' : '0');
            if (formatted && !(c % 4))
                cout << " ";
            m >>= 1;
            c++;
        }
        cout << endl;
    }
    
    void set(unsigned int val) {
        this->val = val;
    }
};

int main(){
    char c1 = 0x5c;
    char c2 = 0x95;
    Bits B(c1);
    B.print();
    B.formatted = true;
    B.print();
    B.set(c2);
    B.print();
    return 0;
}

Posted 2013/04/13 by oop344ed in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: