Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Assign binary value
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 02-04-2006, 05:42 AM #1 manu82 LQ Newbie   Registered: Dec 2005 Posts: 12 Rep: Assign binary value Hi All To assign a value to an integer we use following code snippet in C: int temp = 10; or int temp = 0x0A; or int temp = o12; But can we assign value in Binary format to an Integer data type such as : int temp = b01010
 02-05-2006, 02:04 PM #2 PTrenholme Senior Member Contributing Member   Registered: Dec 2004 Location: Olympia, WA, USA Distribution: Fedora, (K)Ubuntu Posts: 4,186 Rep: Sure. Either use the shift operator on an unsigned int field, or translate your binary value to octal or hex. For you example, something like Code: `unsigned int x = ((1 << 4) + (1 << 2));` should do it. If you've got lots of these to do, you could probably create a macro to accomplish it. Edit: Alternate code: Code: `unsigned int x = ((1 << 4) & (1 << 2)};` Last edited by PTrenholme; 02-05-2006 at 02:06 PM.
 02-06-2006, 10:32 PM #3 manu82 LQ Newbie   Registered: Dec 2005 Posts: 12 Original Poster Rep: yes that's the perfect alternative you have told. But my exact question was does C language has a format for specifying a value as it has for decimal, octal and hexadecimal.
 02-07-2006, 01:42 AM #4 kshkid Member   Registered: Dec 2005 Distribution: RHEL3, FC3 Posts: 383 Rep: no, storing a binary value in a variable as such is not possible in C. Instead, convert the value to binary as a string and store.
02-07-2006, 10:10 AM   #5
PTrenholme
Senior Member

Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep:
Quote:
 Originally Posted by manu82 yes that's the perfect alternative you have told. 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.

Here's a trick you could use:
Code:
```typedef struct {
b1 : 1;
b2 : 1;
b3 : 1;
b4 : 1;} Byte;```
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.

 02-07-2006, 11:10 AM #6 dogpatch Member   Registered: Nov 2005 Location: Central America Distribution: Mepis, Android Posts: 305 Blog Entries: 2 Rep: How about the brute force method: Code: ```/* binbits.h - Represent char type by binary bits */ #define b00000000 0x00 #define b00000001 0x01 #define b00000010 0x02 #define b00000011 0x03 #define b00000100 0x04 #define b00000101 0x05 #define b00000110 0x06 #define b00000111 0x07 #define b00001000 0x08 #define b00001001 0x09 #define b00001010 0x0a #define b00001011 0x0b #define b00001100 0x0c #define b00001101 0x0d #define b00001110 0x0e #define b00001111 0x0f #define b00010000 0x10 #define b00010001 0x11 #define b00010010 0x12 #define b00010011 0x13 #define b00010100 0x14 #define b00010101 0x15 #define b00010110 0x16 #define b00010111 0x17 #define b00011000 0x18 #define b00011001 0x19 #define b00011010 0x1a #define b00011011 0x1b #define b00011100 0x1c #define b00011101 0x1d #define b00011110 0x1e #define b00011111 0x1f #define b00100000 0x20 #define b00100001 0x21 #define b00100010 0x22 #define b00100011 0x23 #define b00100100 0x24 #define b00100101 0x25 #define b00100110 0x26 #define b00100111 0x27 #define b00101000 0x28 #define b00101001 0x29 #define b00101010 0x2a #define b00101011 0x2b #define b00101100 0x2c #define b00101101 0x2d #define b00101110 0x2e #define b00101111 0x2f #define b00110000 0x30 #define b00110001 0x31 #define b00110010 0x32 #define b00110011 0x33 #define b00110100 0x34 #define b00110101 0x35 #define b00110110 0x36 #define b00110111 0x37 #define b00111000 0x38 #define b00111001 0x39 #define b00111010 0x3a #define b00111011 0x3b #define b00111100 0x3c #define b00111101 0x3d #define b00111110 0x3e #define b00111111 0x3f #define b01000000 0x40 #define b01000001 0x41 #define b01000010 0x42 #define b01000011 0x43 #define b01000100 0x44 #define b01000101 0x45 #define b01000110 0x46 #define b01000111 0x47 #define b01001000 0x48 #define b01001001 0x49 #define b01001010 0x4a #define b01001011 0x4b #define b01001100 0x4c #define b01001101 0x4d #define b01001110 0x4e #define b01001111 0x4f #define b01010000 0x50 #define b01010001 0x51 #define b01010010 0x52 #define b01010011 0x53 #define b01010100 0x54 #define b01010101 0x55 #define b01010110 0x56 #define b01010111 0x57 #define b01011000 0x58 #define b01011001 0x59 #define b01011010 0x5a #define b01011011 0x5b #define b01011100 0x5c #define b01011101 0x5d #define b01011110 0x5e #define b01011111 0x5f #define b01100000 0x60 #define b01100001 0x61 #define b01100010 0x62 #define b01100011 0x63 #define b01100100 0x64 #define b01100101 0x65 #define b01100110 0x66 #define b01100111 0x67 #define b01101000 0x68 #define b01101001 0x69 #define b01101010 0x6a #define b01101011 0x6b #define b01101100 0x6c #define b01101101 0x6d #define b01101110 0x6e #define b01101111 0x6f #define b01110000 0x70 #define b01110001 0x71 #define b01110010 0x72 #define b01110011 0x73 #define b01110100 0x74 #define b01110101 0x75 #define b01110110 0x76 #define b01110111 0x77 #define b01111000 0x78 #define b01111001 0x79 #define b01111010 0x7a #define b01111011 0x7b #define b01111100 0x7c #define b01111101 0x7d #define b01111110 0x7e #define b01111111 0x7f #define b10000000 0x80 #define b10000001 0x81 #define b10000010 0x82 #define b10000011 0x83 #define b10000100 0x84 #define b10000101 0x85 #define b10000110 0x86 #define b10000111 0x87 #define b10001000 0x88 #define b10001001 0x89 #define b10001010 0x8a #define b10001011 0x8b #define b10001100 0x8c #define b10001101 0x8d #define b10001110 0x8e #define b10001111 0x8f #define b10010000 0x90 #define b10010001 0x91 #define b10010010 0x92 #define b10010011 0x93 #define b10010100 0x94 #define b10010101 0x95 #define b10010110 0x96 #define b10010111 0x97 #define b10011000 0x98 #define b10011001 0x99 #define b10011010 0x9a #define b10011011 0x9b #define b10011100 0x9c #define b10011101 0x9d #define b10011110 0x9e #define b10011111 0x9f #define b10100000 0xa0 #define b10100001 0xa1 #define b10100010 0xa2 #define b10100011 0xa3 #define b10100100 0xa4 #define b10100101 0xa5 #define b10100110 0xa6 #define b10100111 0xa7 #define b10101000 0xa8 #define b10101001 0xa9 #define b10101010 0xaa #define b10101011 0xab #define b10101100 0xac #define b10101101 0xad #define b10101110 0xae #define b10101111 0xaf #define b10110000 0xb0 #define b10110001 0xb1 #define b10110010 0xb2 #define b10110011 0xb3 #define b10110100 0xb4 #define b10110101 0xb5 #define b10110110 0xb6 #define b10110111 0xb7 #define b10111000 0xb8 #define b10111001 0xb9 #define b10111010 0xba #define b10111011 0xbb #define b10111100 0xbc #define b10111101 0xbd #define b10111110 0xbe #define b10111111 0xbf #define b11000000 0xc0 #define b11000001 0xc1 #define b11000010 0xc2 #define b11000011 0xc3 #define b11000100 0xc4 #define b11000101 0xc5 #define b11000110 0xc6 #define b11000111 0xc7 #define b11001000 0xc8 #define b11001001 0xc9 #define b11001010 0xca #define b11001011 0xcb #define b11001100 0xcc #define b11001101 0xcd #define b11001110 0xce #define b11001111 0xcf #define b11010000 0xd0 #define b11010001 0xd1 #define b11010010 0xd2 #define b11010011 0xd3 #define b11010100 0xd4 #define b11010101 0xd5 #define b11010110 0xd6 #define b11010111 0xd7 #define b11011000 0xd8 #define b11011001 0xd9 #define b11011010 0xda #define b11011011 0xdb #define b11011100 0xdc #define b11011101 0xdd #define b11011110 0xde #define b11011111 0xdf #define b11100000 0xe0 #define b11100001 0xe1 #define b11100010 0xe2 #define b11100011 0xe3 #define b11100100 0xe4 #define b11100101 0xe5 #define b11100110 0xe6 #define b11100111 0xe7 #define b11101000 0xe8 #define b11101001 0xe9 #define b11101010 0xea #define b11101011 0xeb #define b11101100 0xec #define b11101101 0xed #define b11101110 0xee #define b11101111 0xef #define b11110000 0xf0 #define b11110001 0xf1 #define b11110010 0xf2 #define b11110011 0xf3 #define b11110100 0xf4 #define b11110101 0xf5 #define b11110110 0xf6 #define b11110111 0xf7 #define b11111000 0xf8 #define b11111001 0xf9 #define b11111010 0xfa #define b11111011 0xfb #define b11111100 0xfc #define b11111101 0xfd #define b11111110 0xfe #define b11111111 0xff``` (Use a text editor macro to create the above) then, for example: Code: ```#include "binbits.h" unsigned char bin46 = b01000110; unsigned char bin3f = b00111111; unsigned char bin2e = b00101110; int main() { printf ("values are 0x%x, 0x%x, 0x%x\n",bin46,bin3f,bin2e); bin46=b11000001; bin3f=b10100110; bin2e=b00100001; printf ("values are 0x%x, 0x%x, 0x%x\n",bin46,bin3f,bin2e); }``` Pretty klunky, and it only works for 8 bits, but it works. Last edited by dogpatch; 02-07-2006 at 11:15 AM.
 03-30-2013, 09:24 AM #7 ilyas_2011 LQ Newbie   Registered: Mar 2013 Posts: 1 Rep: asm hello, i think inline assembly in c will be useful here. void main() { int a; asm{ mov ax,00111101b; mov a,ax; }; printf("%d", a); }
 03-30-2013, 09:47 AM #8 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: Most C programmers know hex. If you know hex, you don't need binary. Here, print this out and paste it on your table or computer: http://www.mathincomputers.com/uploa...15080_orig.jpg
03-30-2013, 10:48 AM   #9
psionl0
Member

Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep:
Quote:
 Originally Posted by kshkid no, storing a binary value in a variable as such is not possible in C.
Well that is a surprise.
I can think of a number of "masking" situations where writing the bit mask in binary would be the most natural thing to do.

Having to convert the mask to hex first seems sort of primitive.

 03-30-2013, 11:10 AM #10 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: You can store a binary value in a variable in C, and yes you can use a mask to retrieve the value.
03-30-2013, 08:39 PM   #11
psionl0
Member

Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep:
Quote:
 Originally Posted by H_TeXMeX_H 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.

03-30-2013, 10:43 PM   #12
ntubski
Senior Member

Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,383

Rep:
Quote:
Originally Posted by psionl0
Quote:
 Originally Posted by H_TeXMeX_H 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'.

1 members found this post helpful.
03-30-2013, 11:47 PM   #13
psionl0
Member

Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep:
Quote:
 Originally Posted by ntubski 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.

Last edited by psionl0; 03-30-2013 at 11:48 PM.

 03-31-2013, 03:10 AM #14 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: Again, if you are a C programmer you don't need binary, just use hex. If you check the table I posted and the above example you'll see that it's very easy: Code: ```int y = 0x61; check the table and you get: 0x6 = 0110 0x1 = 0001 so put those together and you get what you are supposed to: 0b01100001``` Now, I could get into endianness, but I'll just leave it to you to read: http://en.wikipedia.org/wiki/Endian#...C0Dh_in_memory 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.
 03-31-2013, 12:22 PM #15 theNbomr LQ 5k Club   Registered: Aug 2005 Distribution: OpenSuse, Fedora, Redhat, Debian Posts: 5,398 Blog Entries: 2 Rep: 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. --- rod. Last edited by theNbomr; 03-31-2013 at 12:24 PM.

 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 Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jtir Solaris / OpenSolaris 1 09-28-2005 03:37 AM fuelinjection Linux - Networking 8 11-28-2003 03:37 AM whitmell Linux - Newbie 4 09-16-2003 07:36 AM Xiangbuilder Linux - Newbie 2 08-31-2003 07:46 AM pilot1 Linux - Networking 2 12-06-2002 03:37 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:01 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -