Skip to main content
Mod Moved Comments To Chat
fixed initial mask value per Peter Cordes
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 0x80;1U << (CHAR_BIT-1); mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(const unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 0x80; mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(const unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 1U << (CHAR_BIT-1); mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(const unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
changed "strongly" to "statically" to satisfy Konrad Rudolph, and mask calculation per Peter Cordes
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
for(unsigned char mask = 0x80;1U << (CHAR_BIT-1); mask; mask >>= 1) 

C is a stronglystatically typed language, so it is neither necessary nor desirable to encode the type within the name. See NL.5 for more. (Those are C++ guidelines, but this is equally applicable to C.)

for(unsigned char mask = 0x80; mask; mask >>= 1) 

C is a strongly typed language, so it is neither necessary nor desirable to encode the type within the name. See NL.5 for more. (Those are C++ guidelines, but this is equally applicable to C.)

for(unsigned char mask = 1U << (CHAR_BIT-1); mask; mask >>= 1) 

C is a statically typed language, so it is neither necessary nor desirable to encode the type within the name. See NL.5 for more. (Those are C++ guidelines, but this is equally applicable to C.)

fixed missing `const` in loop
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 0x80; mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(const unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 0x80; mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
#include <stddef.h>
#include <stdio.h>
#include <limits.h>

void toBinary(const unsigned char *ptr) {
    for(unsigned char mask = 0x80; mask; mask >>= 1) {
        putchar(*ptr & mask ? '1' : '0');
    }
}

void binary(const void* p, size_t nBytes) {
    for(const unsigned char* ptr = p; nBytes; --nBytes) {
        putchar('[');
        toBinary(ptr++);
        putchar(']');
        if(nBytes > 1) {
            putchar('\t');
        }
    }
    putchar('\n');
}

#define BINARY(x) binary(&x, sizeof x)

int main(void) {
    char a = 85;
    int b = 0x12345678;
    BINARY(a);
    BINARY(b);
}
made code formatting consistent
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
fixed two minor typos
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading