Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- * maximum bit size
- * for different
- * sizes of numbers
- */
- #define MAX_BITS 32
- /*
- * check to see if the
- * given bit is set (1)
- * or not (0)
- */
- int is_bit (int i) {
- return i & 0x01;
- }
- /*
- * bit shift each bit
- * in number and check
- * if bit is set (1) or
- * not (0) and put into
- * array
- */
- void dec_to_bin (int n, char *buf) {
- int i;
- for (i = 0; i < MAX_BITS; i++) {
- buf[MAX_BITS-i-1] = (is_bit (n >> i)) ? '1' : '0';
- }
- }
- /*
- * print bits with
- * iteration
- */
- int print_bits (const char *buf) {
- int i;
- for (i = 0; i < MAX_BITS; i++) {
- if (i != 0 && i % 8 == 0) {
- putchar (' ');
- }
- putchar (buf[i]);
- }
- putchar ('\n');
- return i;
- }
- int main (int argc, char *argv[]) {
- char buf[MAX_BITS];
- memset (buf, '0', MAX_BITS);
- dec_to_bin (atoi (argv[1]), buf);
- printf ("%d to bits: ", atoi (argv[1]));
- print_bits (buf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement