binary const

S

serrand

Hello all,

is there a way in order to write binary numbers as hexa or octal in c ?

Xavier
 
R

Robert Gamble

serrand said:
Hello all,

is there a way in order to write binary numbers as hexa or octal in c ?

I assume you are asking whether C supports a binary constant notation
like it does for octal and hexadecimal numbers, the answer is no. It
is not too difficult to create macros that allow you to do this though.

Robert Gamble
 
J

Joe Wright

Robert said:
I assume you are asking whether C supports a binary constant notation
like it does for octal and hexadecimal numbers, the answer is no. It
is not too difficult to create macros that allow you to do this though.

Robert Gamble
Can you give me an example of a macro to do this? Thanks.
 
R

Robert Gamble

Joe said:
Can you give me an example of a macro to do this? Thanks.

Absolutely. I have seen a couple of variations (which I can't seem to
locate right now) but the below example gets the gist of the technique
across:

#include <stdio.h>

#define A(x) 0 ## x ## ULL
#define B(x) ((A(x) & 01ULL) + (2*(!!((A(x) & 010ULL)))) + (4*(!!((A(x)
& 0100ULL)))))

int main (void) {
printf("%llu\n", B(110));
return 0;
}

This version handles up to 3 digit binary numbers, extending it to
handle more is trivial.
This isn't perfect as it can only represent a range limited by the
highest octal representation which is why the ULL modifier is there but
it is good enough for some.

Robert Gamble
 
K

Keith Thompson

Robert Gamble said:
Absolutely. I have seen a couple of variations (which I can't seem to
locate right now) but the below example gets the gist of the technique
across:

#include <stdio.h>

#define A(x) 0 ## x ## ULL
#define B(x) ((A(x) & 01ULL) + (2*(!!((A(x) & 010ULL)))) + (4*(!!((A(x)
& 0100ULL)))))

int main (void) {
printf("%llu\n", B(110));
return 0;
}

This version handles up to 3 digit binary numbers, extending it to
handle more is trivial.
This isn't perfect as it can only represent a range limited by the
highest octal representation which is why the ULL modifier is there but
it is good enough for some.

That's a very clever technique. Note that "very clever" is not
necessarily a good thing.

Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible, you
are, by definition, not smart enough to debug it.
-- Brian W. Kernighan

(Yes, that's the K in K&R.)

It would be nice, IMHO, if C supported binary constants, probably
using a syntax like 0b11001001. Since it doesn't, the best
alternative is to use octal or hexadecimal constants; a knowledgable
reader knows that each digit represents 3 or 4 bits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top