All pastes #227067 Raw Edit

Untitled

public text v1 · immutable
#227067 ·published 2006-10-29 00:43 UTC
rendered paste body
#include <stdio.h>

void bin_prnt_byte(int x);
void bin_prnt_int(int x);

int main(int argc, char **argv) {
        unsigned int num = atoi(argv[1]);

        int i;

        int powoftwo = 0;

        for (i = 0; i < 32; i++) {
                num >>= 1;
                bin_prnt_int(num);
                if (num & 1) {
                        printf(" & 1 == true\n", num);
                        powoftwo++;
                } else {
                        printf(" & 1 == false\n", num);
                }
                if (powoftwo > 1) {
                        printf("nope\n");
                        return 1;
                }
        }
        printf("%d\n", powoftwo);
        if (powoftwo == 1) {
                printf("yep\n");
                return 0;
        } else {
                printf("nope\n");
                return 1;
        }
}
void bin_prnt_byte(int x) {
        int n;
        for(n=0; n<8; n++) {
                if((x & 0x80) !=0) {
                        printf("1");
                } else {
                        printf("0");
                }
        if (n==3) {
                printf(" "); /* insert a space between nybbles */
        }
        x = x<<1;
}

void bin_prnt_int(int x) {
        int hi, lo;
        hi=(x>>8) & 0xff;
        lo=x&0xff;
        bin_prnt_byte(hi);
        printf(" ");
        bin_prnt_byte(lo);
}