While reading a book called "Let us C" I read that a function showbit()
exists which can show you the bits of the number. There wasn't any special header file mentioned for it. Searched for it on the internet and didn't found anything useful. Is there such a function? I want this to print the binary of decimal numbers. Else please give me a replacement function. Thanks

- 10,958
- 2
- 46
- 71
-
Search again: http://www.google.com/search?q=showbits+c – dalle Oct 02 '10 at 07:24
-
6You should probably avoid this and other Kanetkar books - they are full of errors and misinformation and are also seriously out of date. – Paul R Oct 02 '10 at 07:53
-
@PaulR:Can you suggest me some good books?I also read Robert Lafore's Turbo C. – Oct 02 '10 at 11:35
-
@fahad: there are some good book recommendations right here on Stack Overflow, including free books and online tutorials - search for "C books" – Paul R Oct 02 '10 at 12:41
-
3@fahad: See "The Definitive C Book Guide and List" - http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – schot Oct 02 '10 at 13:23
-
A book on C by Al Kelley and Ira Pohl. My brother gave it to me. I read it after let us C. It is worth the read. – Ram Nov 18 '11 at 09:35
-
The book doesn't say that the function already exists. It has given the function definition after explaining the bitwise operators in C – Gaurav Kalra Jan 08 '12 at 14:58
-
@GauravKalra: I will have a look at it again and get back to you on this. – Jan 09 '12 at 08:54
8 Answers
All integers are actually in binary in your computer. Its just that it is turned into a string that is the decimal representation of that value when you try to print it using printf and "%d". If you want to know what it looks like in some other base (e.g. base 2 or binary), you either have to provide the proper printf format string if it exists (e.g. "%x" for hex) or just build that string yourself and print it out.
Here is some code that can build the string representation of an integer in any base in [2,36].
#include <stdio.h>
#include <string.h>
char digits[]="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void reverse(char* start, char* end)
{
for(end--;start<end;start++,end--)
{
char t=*start;
*start=*end;
*end=t;
}
}
int base_change(int n, int base,char* buffer)
{
int pos=0;
if (base>strlen(digits))
return -1;
while(n)
{
buffer[pos]=digits[n%base];
n/=base;
pos++;
}
buffer[pos]='\0';
reverse(buffer,buffer+pos);
return 0;
}
int main()
{
char buffer[32];
int conv=base_change(1024,2,buffer);
if (conv==0) printf("%s\n",buffer);
return 0;
}

- 26,140
- 11
- 55
- 86
-
1@fahad: huh? What part of `n/=base` do you not understand? Please lookup shortcuts such as `+=`, `*=`, `/=` etc. – MAK Oct 02 '10 at 17:47
You can also try this snippet which uses bit-shifting:
EDIT: (I've made it more portable)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BITS_IN_BYTE 8
#define INTEGRAL_TYPE unsigned int
void showBits(INTEGRAL_TYPE x) {
int i;
static int intSizeInBits = sizeof(INTEGRAL_TYPE) * BITS_IN_BYTE;
static char symbol[2] = {'0','1'};
char * binary = (char*) malloc(intSizeInBits + 1);
memset(binary, 0, intSizeInBits + 1);
for (i=0; i< intSizeInBits; i++) {
binary[intSizeInBits-i-1] = symbol[(x>>i) & 0x01];
}
printf("%s\n", binary);
free(binary);
}
int main() {
showBits(8698513);
return 0;
}
HTH!

- 10,935
- 5
- 50
- 70
This is a very simple solution for printing the bits of an integer
int value = 14;
int n;
for (n=8*sizeof(int)-1;n>=0;n--) {
printf("%d",(value >>n)&1);
}

- 6,870
- 5
- 36
- 71
The book "Let Us C" doesn't define it as a built-in function. The showbits() function is defined in later part of the book as a complete user defined function as I personally went through it. This answer is to anyone who haven't reached that later part of the book and are stuck at the first appearance of the function in the book.

- 893
- 1
- 6
- 15
this is the header file for showbits
void showbits(unsigned int x)
{
int i;
for(i=(sizeof(int)*5)-1; i>=0; i--)
(x&(1u<<i))?putchar('1'):putchar('0');
printf("\n");
}
-
1This uses `?:` in void context (bad style) and (for some reason) only prints bits 0 to 19 (or 39, depending on your platform). – melpomene Nov 11 '17 at 16:49
you need to look here
@downvoter It works fine in c also. You just need to reformat your code in c-style.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char buffer[20];
int i = 3445;
_itoa( i, buffer, 2 );
printf("String of integer %d (radix 2): %s",i,buffer);
return 0;
}
*you need to save your file as .c in MSVC++ for _itoa() to work.*

- 1,667
- 6
- 20
- 26
-
1As the forum thread you link to goes on to say, `_itoa` isn't part of the standard: its' Microsoft-specific. As _waItp_ says, *"ANSI C only defines ato?() functions, not the other way around. **MS usually puts an underscore in front of functions that are not ANSI Standard, whereas Borland does not**. Many compilers though have defined ?toa() functions, but you can't rely on them for portability."* – gamen Nov 20 '11 at 12:42
No there is no pre built function and you do not need to include any specific header file. You will have to provide implementation for function showbits(int).
#include <stdio.h>
void showbits(int);
int main()
{
int j,k;
for(j=0;j<=11;j++)
{
printf("\nBinary value of decimal %d is :",j);
showbits(j);
}
return 0;
}
showbits(int n){
int i,k,andmask;
for(i = 15;i>=0;i--){
andmask = 1<<i;
k = n & andmask;
k==0 ? printf("0"):printf("1");
}
}

- 69
- 3
- 8
-
1This is missing a return type (an error since C99), uses `?:` in void context (bad style), and only prints bits 0 to 15. – melpomene Nov 11 '17 at 16:50
If you want to print out the bits of a float, for example you could do something like:
float myFloat = 45.2;
for (int n=0;n<8*sizeof(float);n++) {
printf("%d",(myFloat>>n)&1);
}

- 6,134
- 4
- 33
- 55
-
-
You recognised it though. It shifts down each bit to the bottom, then isolated it with &&1. This gives a 0 or 1. – Alexander Rafferty Oct 02 '10 at 07:27
-
2gcc says "error: invalid operands to binary >> (have ‘float’ and ‘int’)". – Edgar Bonet Oct 02 '10 at 07:43
-
1Replace: float -> int, %f -> %d, && -> & and run the loop backwards, then it works as intended. – Edgar Bonet Oct 02 '10 at 07:48
-
3@Alex: Does this actually compile on your system, much less function correctly? – Potatoswatter Oct 02 '10 at 08:38
-
It's just a snippet, so I haven't compiled it. But I see no reason for it not to. – Alexander Rafferty Oct 02 '10 at 09:00
-
3@Alexander: what about the fact that each of the operands of a shift expression shall have integer type according to ISO C99 6.5.7.2? – ninjalj Oct 02 '10 at 11:59