473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

binary const

Hello all,

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

Xavier
Jan 4 '06 #1
5 6727
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

Jan 5 '06 #2
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 ---
Jan 5 '06 #3
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

Jan 5 '06 #4
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

Jan 5 '06 #5
"Robert Gamble" <rg*******@gmail.com> 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) ks***@mib.org <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.
Jan 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andrew | last post by:
Hi I have a question is there a function in C++ to convert an integer into a Binary number Thanks in Advance Cheers
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
2
by: Bastian Voigt | last post by:
Hallo List! I just found this old posting on google. Now my question is how can I read an integer value from the PGresult using the binary format? Can someone plz gimme a code example? (please...
1
by: Alex Buell | last post by:
Right... here it is - flames and comments all welcome: #ifndef __BINARY__ #define __BINARY__ #include <iostream> #include <string> #include <limits> #include <exception>
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.