Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

hurrf durf

Name: Anonymous 2010-01-15 11:31

I suck at bit twiddling, somebody please explain why this works.

// to-binary-bitops.cpp  Print binary representation of ints
// Fred Swartz  - 2001-09-04

#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        cout << "decimal: " << n << endl;

        // print binary with leading zeros
        cout << "binary : ";
        for (int i=31; i>=0; i--) {
            int bit = ((n >> i) & 1);
            cout << bit;
        }
        cout << endl;
    }//end loop
    return 0;
}//end main

Name: Anonymous 2010-01-15 15:59

This is disappointing.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List