473,287 Members | 1,629 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,287 software developers and data experts.

literal binary?

Is there any way to represent a literal numerical value in binary?
My wish would be something like 0b0011 instead of the hex 0x0F.
( if this is even the same. )

Sometimes I find it's easier to use my finger's than my brain.

Nov 14 '05 #1
3 14321
Stephen Mayes wrote:
Is there any way to represent a literal numerical value in binary?
My wish would be something like 0b0011 instead of the hex 0x0F.
( if this is even the same. )
There is not. Hexadecimal or octal are your options.
Sometimes I find it's easier to use my finger's than my brain.

Oh.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
"Stephen Mayes" <de************@themayeshouse.us> wrote:
Is there any way to represent a literal numerical value in binary?
My wish would be something like 0b0011 instead of the hex 0x0F.
( if this is even the same. )

Sometimes I find it's easier to use my finger's than my brain.


Tom Torfs donated some macros awhile back. His Usenet post is quoted
below.

================================================== ===================
From: tomto...@village.uunet.be (Tom Torfs)
Newsgroups: comp.lang.c,comp.arch.embedded
Subject: Binary constant macros
Date: 26 Feb 2004 07:36:35 -0800
Organization: http://groups.google.com
Lines: 66
Message-ID: <b9**************************@posting.google.com >
NNTP-Posting-Host: 146.103.254.11
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1077809795 7936 127.0.0.1 (26 Feb 2004
15:36:35 GMT)
X-Complaints-To: groups-ab...@google.com
NNTP-Posting-Date: Thu, 26 Feb 2004 15:36:35 +0000 (UTC)

Hello All,

I've been missing the lack of support for binary numeric literals in
C. To get around it I wrote the following handy macros, which allows
you to simply write something like:

whatever = B8(10101010);

and will translate as:

whatever = 85;

(compile-time constant)

Code below... hopefully it's useful to some of you as well.

greetings,
Tom

/* Binary constant generator macro
By Tom Torfs - donated to the public domain
*/

/* All macro's evaluate to compile-time constants */

/* *** helper macros *** /

/* turn a numeric literal into a hex constant
(avoids problems with leading zeroes)
8-bit constants max value 0x11111111, always fits in unsigned long
*/
#define HEX__(n) 0x##n##LU

/* 8-bit conversion function */
#define B8__(x) ((x&0x0000000FLU)?1:0) \
+((x&0x000000F0LU)?2:0) \
+((x&0x00000F00LU)?4:0) \
+((x&0x0000F000LU)?8:0) \
+((x&0x000F0000LU)?16:0) \
+((x&0x00F00000LU)?32:0) \
+((x&0x0F000000LU)?64:0) \
+((x&0xF0000000LU)?128:0)

/* *** user macros *** /

/* for upto 8-bit binary constants */
#define B8(d) ((unsigned char)B8__(HEX__(d)))

/* for upto 16-bit binary constants, MSB first */
#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) \
+ B8(dlsb))

/* for upto 32-bit binary constants, MSB first */
#define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \
+ ((unsigned long)B8(db2)<<16) \
+ ((unsigned long)B8(db3)<<8) \
+ B8(dlsb))

/* Sample usage:
B8(01010101) = 85
B16(10101010,01010101) = 43605
B32(10000000,11111111,10101010,01010101) = 2164238933
*/

greetings,
Tom
================================================== ===================

--
Dan Henry
Nov 14 '05 #3
Stephen Mayes <de************@themayeshouse.us> wrote:
Is there any way to represent a literal numerical value in binary?
My wish would be something like 0b0011 instead of the hex 0x0F.
( if this is even the same. )


Well, 0x0F would be "0b01111" if such a thing would exist;-)
But since, at least in my experience, when you deal with single
bits like that the bits typically have a certain meaning (e.g.
they are standing for a on/off flags etc., put together into
a single value) what about defining meaningful names for them
and then combining them with ORs? Like in e.g.

#define WRONLY ( 1 << 0 )
#define RDWR ( 1 << 1 )
#define CREAT ( 1 << 7 )

Thereby you could use ( CREAT | RDWR ) instead of 0x82 (or, even
harder to read, "0b10000010" as you to wish for) and would have
the additional ad- vantage of making the code easier to under-
stand. The program won't get slowed down by that because the
compiler should be able to replace that by the corresponding
constant.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

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

Similar topics

5
by: Ravi | last post by:
I recently had to write some code which required me to use a binary search tree (BST) due to running time concerns. I found that there is no BST class in the Standard C++ library. I have been told...
18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
6
by: Kelvin | last post by:
Hi everyone: when we wanna use hex numbers in C, we usually write something like: int hex_num = 0x12F9; but how can I declare a binary number in a similar way by putting some leading words to...
1
by: Seth King | last post by:
I am used to programming in embedded C and I want to input a binary number into an int. usually I would just use bin as a suffix or b as a prefix int x = b001 or int x = 001110bin the hex...
11
by: valerij | last post by:
Hi, what is the best way to hardcode into a C/C++ program binary values/constants/etc. Since: int a; a=0b0111; is wrong, how should I do it. I thought about converting them first to...
19
by: sethukr | last post by:
Hi everybody, Can we assign binary values to an integer variable like assigning Hexadecimal values??? Thanks, Sethu
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
24
by: Bartc | last post by:
The stdin/stdout files of C seem to be always in Text mode. Is there any way of running a C program so that these (but especially stdout) are in Binary mode instead? (I'm in the process of...
33
by: bearophileHUGS | last post by:
I have just re-read the list of changes in Python 2.6, it's huge, there are tons of changes and improvements, I'm really impressed: http://docs.python.org/dev/whatsnew/2.6.html I'll need many...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.