Hi!
>
How can I output value of char or int in binary form with printf(); ?
>
thanx in advance
There is no standard format specifier for binary form. You will have
to do the conversion manually, testing each bit from highest to
lowest, printing '0' if it's not set, and '1' if it is.
On Sat, 24 Mar 2007 08:55:36 +0100, Carramba <user@example.n etwrote:
>Hi!
>
>How can I output value of char or int in binary form with printf(); ?
>
>thanx in advance
The C Standards do not define a conversion specifier for printf() to
output in binary. The only portable way to do this is to roll your
own. Here's a start:
printf("%s\n", int_to_binary_s tring(my_int));
Make sure when you implement int_to_binary_s tring() that it works with
most desktop targets where sizeof(int) * CHAR_BIT = 32 as well on many
embedded targets where sizeof(int) * CHAR_BIT = 16.
>Hi!
>>
>How can I output value of char or int in binary form with printf(); ?
>>
>thanx in advance
There is no standard format specifier for binary form. You will have
to do the conversion manually, testing each bit from highest to
lowest, printing '0' if it's not set, and '1' if it is.
thanx, maybe you have so suggestion or link for further reading on how
to do it?
Hi!
>
How can I output value of char or int in binary form with printf(); ?
>
thanx in advance
There is no standard format specifier for binary form. You will have
to do the conversion manually, testing each bit from highest to
lowest, printing '0' if it's not set, and '1' if it is.
thanx, maybe you have so suggestion or link for further reading on how
to do it?
Others have given code already, but here's mine anyway:
#include <limits.h>
#include <stdio.h>
void print_char_bina ry(char val)
{
char mask;
if(CHAR_MIN < 0)
{
if(val < 0
|| val == 0 && val & CHAR_MAX)
putchar('1');
else
putchar('0');
}
"Carramba" <user@example.n etschrieb im Newsbeitrag
news:46050ac1$0 $492$cc7c7865@n ews.luth.se...
thanx ! have few questions about this code :)
Malcolm McLean wrote:
>>
>"Carramba" <user@example.n etwrote in message
>news:4604d5ca$ 0$488$cc7c7865@ news.luth.se...
>>Hi!
>>>
>>How can I output value of char or int in binary form with printf(); ?
>>>
>>thanx in advance
>#include <limits.h>
>/*
> convert machine number to human-readable binary string.
> Returns: pointer to static string overwritten with each call.
>*/
>char *itob(int x)
>{
> static char buff[sizeof(int) * CHAR_BIT + 1];
why sizeof(int) * CHAR_BIT + 1 ? what does it mean?
If you want to put an in't binary representation ionto a string you need
that much space.
On many implementations sizeof(int) is 4 and CHAR_BIT is 8, so you'd need an
array of 33 chars (including the teminating null byte).
I'd use sizeof(x) instead of sizeof(int), that way you can easily change the
function to work on e.g. long long
> int i;
> int j = sizeof(int) * CHAR_BIT - 1;
why sizeof(int) * CHAR_BIT - 1 ? what does it mean?
Arrays count from 0 to n and the terminating null byte isn't needed , so the
index goes from 0 to 31 (assuming the same sizes as above)
>
There are some problems with that shift expression.
>
(1 << sizeof(int) * CHAR_BIT - 1) is undefined.
>
The function should take an unsigned int. However I didn't want to add that
complication for the OP. It should work OK on almost every platform.
--
Free games and programming goodies.
That makes no difference.
The evaluation of (1 << sizeof(int) * CHAR_BIT - 1)
in a program is always undefined,
and prevents a program from being a "correct program".
(1 << sizeof(int) * CHAR_BIT - 1) can't be a positive value.
However I didn't want to add that
complication for the OP.
That expression would be perfect to use as
an example of how not to write code.
>>
>"pete" <pfiland@mindsp ring.comwrote in message
>
(1 << sizeof(int) * CHAR_BIT - 1) is undefined.
>
>The function should take an unsigned int.
>
That makes no difference.
The evaluation of (1 << sizeof(int) * CHAR_BIT - 1)
in a program is always undefined,
and prevents a program from being a "correct program".
(1 << sizeof(int) * CHAR_BIT - 1) can't be a positive value.
>
>However I didn't want to add that
>complication for the OP.
>
That expression would be perfect to use as
an example of how not to write code.
>
>It should work OK on almost every platform.
>
(1u << sizeof(int) * CHAR_BIT - 1) is defined.
>
Your initial value of j is also wrong:
>
int j = sizeof(int) * CHAR_BIT - 1;
>
buff[j] = 0;
for(i=0;i<sizeo f(int) * CHAR_BIT; i++)
{
if(x & (1 << i))
buff[j] = '1';
else
buff[j] = '0';
>
As you can see in your code above,
the first side effect of the for loop,
is to overwrite the null terminator.
>
>
/* BEGIN new.c */
>
#include <stdio.h>
#include <limits.h>
>
char *itob(unsigned x);
>
int main(void)
{
printf("%s\n", itob(100));
return 0;
}
>
char *itob(unsigned x)
{
unsigned i;
unsigned j;
static char buff[sizeof x * CHAR_BIT + 1];
>
j = sizeof x * CHAR_BIT;
buff[j--] = '\0';
for (i = 0; i < sizeof x * CHAR_BIT; i++) {
if (x & (1u << i)) {
buff[j--] = '1';
} else {
buff[j--] = '0';
}
if ((1u << i) == UINT_MAX / 2 + 1) {
break;
}
}
while (i++ < sizeof x * CHAR_BIT) {
buff[j--] = '0';
}
return buff;
}
>
/* END new.c */
>
unsigned integers aren't allowed padding bits so you don't need all that
complication.
A pathological platform might break on the expression 1 << int bits - 1,
agreed. To be strictly correct we need to do the calculations in unsigned
integers, but I've explained why I didn't do that.
The off by one error in writing the nul was a slip. Of course I didn't
realise because the static array was zero intilaised anyway. So well
spotted.
--
Free games and programming goodies.
Wrong again.
unsigned char isn't allowed padding bits.
UINT_MAX is allowed to be as low as INT_MAX,
and you can't achieve that without padding bits
in the unsigned int type.
Comment