Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > binary const

Reply
Thread Tools

binary const

 
 
serrand
Guest
Posts: n/a
 
      01-04-2006
Hello all,

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

Xavier
 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      01-05-2006
serrand wrote:
> 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

 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      01-05-2006
Robert Gamble wrote:
> serrand wrote:
>
>>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
>

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

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      01-05-2006
Joe Wright wrote:
> Robert Gamble wrote:
> > serrand wrote:
> >
> >>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
> >

> 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

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      01-05-2006
Robert Gamble wrote:
> Joe Wright wrote:
> > Robert Gamble wrote:
> > > serrand wrote:
> > >
> > >>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
> > >

> > 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)


Found it. Tom Torfs posted virtually the same method on
comp.arch.embedded on 2/26/2004 in the topic entitled "Binary constant
macros"
(http://groups.google.com/group/comp....30b6d3da12c8f).
My version was based off what I had remembered of his very clever
idea, Tom's is much nicer.

Robert Gamble

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
"Robert Gamble" <(E-Mail Removed)> writes:
> Joe Wright wrote:
>> Robert Gamble wrote:
>> > serrand wrote:
>> >>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
>> >

>> 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.


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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments