ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
But my exact question was does C language has a format for specifying a value as it has for decimal, octal and hexadecimal.
No, except, of course, on a binary computer, an unsigned int is -- by definition -- a binary value. So (obviously) any assignment to an unsigned int creates a binary value.
which defines a "Byte" type with each "bit" labeled for your use.
Edit: Do be aware that bit addressing (as above) can be very machine dependent, and any code using such constructs may (and probably will) not be portable to any other CPU type.
Last edited by PTrenholme; 02-07-2006 at 10:16 AM.
You can store a binary value in a variable in C, and yes you can use a mask to retrieve the value.
Are you arguing a strawman? You can enter a number in decimal, octal or hexadecimal but not binary.
This is just a matter of terminology. I would say there is no such thing as a binary value: binary is just a way to encode a value.
Code:
int x = 97;
int y = 0x61;
int z = 0b1100001; // 0b is a GNU C extension (I think it didn't exist when this thread was first posted)
In the above code x, y and z all have the same value; x doesn't have a decimal value, y doesn't have a hexadecimal value, nor does z have a binary value. On the other hand, "binary value" is sometimes used to mean "not a text value", which of course somewhat depends on what you use the value for: eg 97 can mean 'a'.
This is just a matter of terminology. I would say there is no such thing as a binary value: binary is just a way to encode a value.
I think the average programmer here would realize that no matter what base you enter an int it gets stored as a 32-bit word (unless short or long are used).
Code:
int z = 0b1100001; // 0b is a GNU C extension (I think it didn't exist when this thread was first posted)
That's a relief. Considering that C was originally designed to be 1 step higher than assembly language, I'm surprised that encoding an int in binary took this long.
EDIT:
Just so you don't say I'm "strawman"ing again, here's how you can get individual bits using a mask:
Code:
int y = 0x61;
first = y & 0x01;
second = y & 0x02;
third = y & 0x04;
...
You can also use shifts if you prefer:
first = y & (1 << 0);
second = y & (1 << 1);
third = y & (1 << 2);
If you wanted to use these as binary values you would have to right shift them to the first digit,
but really anything positive would be 1 in that location and anything 0 is 0.
Last edited by H_TeXMeX_H; 03-31-2013 at 03:18 AM.
The real question should be worded like 'Is there a C language notation that expresses an integer in the binary radix?'. Simple answer is 'no'. There are all sorts of work-arounds, as people have pointed out. There are also some replies above that are just nonsensical.
Most programmers are good enough at the mental gymnastics of converting hexadecimal notation to binary bit patterns that it doesn't matter. If there is some information to be conveyed in the source code that certain bits are to be set or cleared, then expressing the value in some kind of binary notation isn't very good anyway, as a string of '0's and '1's is very difficult to read and write accurately. Use a macro like setBit() or clearBit() to make the meaning clear, or use a #defined constant with a meaningful name instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.