im trying to figure out how to do it in binary
int foo = b'0011011;
or something whats teh syntax for it
im trying to figure out how to do it in binary
int foo = b'0011011;
or something whats teh syntax for it
You cannot do this in C like you can with decimal, hex, or octal.
Last edited by kermit; 09-17-2009 at 05:35 PM.
A very good reminder. I had completely forgot about that function of strtol (Ironically, I just posted a link on another thread regarding the use of strtol to get integer input). What I had been thinking was something along these lines:
This was something along the lines of what the OP was after I think.Code:#include <stdio.h> int main(void) { int x = 0xdd; int y = 023; int z = b01001; /* oops - no such beast! */ printf("x = %d\ny = %d\nz = %d\n", x, y, z); return 0; }
Yeah, I had to go look. I couldn't remember if you could do base 2 or not. Apparently anything from 2 to 36 is valid.
Quzah.
Hope is the first step on the road to disappointment.
Is this what you're trying to get at
Code:int foo = 0x1b; /* stored inside the machine as binary 0011011 */
It always seemed kind of strange to me why not offer a direct syntax in C to do what the OP posted
That and no %b in printf() for printing in binary numbers. I guess the designers of C would be able to read hex numbers as if they were binary, and convert between them in moments.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
gcc has an extension for binary numbers with a 0b prefix. I haven't tried to use it, though. (And of course printf extensions are up to the library people, etc etc etc. Don't know of any binary printers, but there might be some out there.)
Yep! it would be nice to switch the input base kinda like "bc", which is written in C and uses C-style syntax.