Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

to print 1101 instead of 13 (bits) 1

Status
Not open for further replies.

this1kid

Programmer
Jul 12, 2002
2
US
Hi,

Does C has built-in functions to output an integer in binary form and hex form?

On a side-note, I found this statement in "The C Progging Lang" by Brian W. Kernighan and Dennis M. Ritchie:
x = x & ~077;
Apparently, it sets the last six bits of x to zero... but I'm kinda puzzled about the "077" since it doesn't seem like it's a hexadecimal number. Can someone explain that please?

Thanks!
 
Ok.. I got the part about the hex format.. ironically, from the book I mentioned in my first article:

%x : hexadecimal int, w/ or w/o leading 0x or 0X; requires int *

I'm still befuddled about how to output ints in binary form and the meaning of ~077, though.
 
Any number precedded by 0 is interpreted as an octal value. `~' is C's one's complement operator.

As for outputting ints as binary write a small a small function. There is no way of doing it C.
 
if you are working on unix, you could use the /bin/dc or /bin/bc
to convert decimals to an other base, so if you type:
echo "ibase=10;obase=16;17"|bc
you get: 11
try the same and append -c to get the syntax for dc, so:
echo "10i\n16o\n17ps.\n" | dc
gives you the same result.
the way back: ibaseXX -> obase10 does not work.
you also could get the src of dc.c and bc.c (internet) and compile your own code.
this two are sure able to convert: bin > int and int > bin... i just not know, how to
say them to do this !!
about 10 years ago, i wrote my own c-code to do this job, send me an email
if you are interested.
jean-michel.sarbach@ubs.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top