424,946 Members | 709 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

# Binary values assignment

 P: n/a Hi everybody, Can we assign binary values to an integer variable like assigning Hexadecimal values??? Thanks, Sethu May 8 '07 #1
19 Replies

 P: n/a Addition to that question : int a = 0xef; instead of this, can we assign the direct binary value 1110 1111 to 'a' ??? -Sethu May 8 '07 #2

 P: n/a se*****@gmail.com said: Can we assign binary values to an integer variable like assigning Hexadecimal values??? C does not provide support for numeric literals expressed in binary representation. Nevertheless, it is possible to hack something up. Probably the best way is something simple such as: int a = strtoul("11101111", NULL, 2); but you could always do something like: #define BINARY_8 ((((((((0 #define O )*2 #define l )*2+1 int a = BINARY_8 l l l O l l l l ; -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. May 8 '07 #3

 P: n/a se*****@gmail.com wrote: Addition to that question : int a = 0xef; instead of this, can we assign the direct binary value 1110 1111 to 'a' ??? No. Why do you want to? [`int a = 0xef;` /does/ initialise-with-the-binary-value-1110_1111 `a`.] -- "Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/ Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England May 8 '07 #4

 P: n/a Chris Dollin Addition to that question :int a = 0xef;instead of this, can we assign the direct binary value 1110 1111to 'a' ??? No. Why do you want to? [`int a = 0xef;` /does/ initialise-with-the-binary-value-1110_1111 `a`.] The same argument could be used against hexadecimal literals; after all int a = 0xef; is the same as int a = 239; The language doesn't support binary constants because the standard doesn't define them. But IMHO it would be a good thing if it did. Suggested syntax: 0b11101111. For long literals, it would be nice to allow some sort of grouping syntax; there's precedent in other languages for using '_' for this. And there's no good reason not to allow '_' in integer constants in general. For example: unsigned int a = 0xdead_beef; int b = 0b1110_1111; int c = 123_456_789; I think the reason binary constants have never been added is that hexadecimal constants serve (most of) the same purpose. In a hex constant, each digit corresponds exactly to 4 binary digits, which is not the case for decimal constants. So binary constants wouldn't be all that big an improvement. On the other hand, if a future version of the standard were to add binary constants, it would be many years before they could be used with any assurance of portability. But it's something to keep in mind when designing new languages. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" May 8 '07 #5

 P: n/a Keith Thompson wrote: Chris Dollin se*****@gmail.com wrote: >>Addition to that question :int a = 0xef;instead of this, can we assign the direct binary value 1110 1111to 'a' ??? No. Why do you want to?[`int a = 0xef;` /does/ initialise-with-the-binary-value-1110_1111 `a`.] The same argument could be used against hexadecimal literals; after all int a = 0xef; is the same as int a = 239; I don't think chris.dollin was arguing against binary literals, but making it clear that the hex literal expressed the same value -- and the same bit-pattern (as far as C can see) -- as binary 1110_1111. And I believe the question "why do you want to?" is a real rather than rhetorical one: what is the OPs reason for wanting binary literals? (We have, I think, seen people confused about decimal vs binary vs hex before -- it's /possible/ that the OP is simply confused about the meaning of different bases.) The language doesn't support binary constants because the standard doesn't define them. But IMHO it would be a good thing if it did. Dunno. Not really sure there's enough call for it. Suggested syntax: 0b11101111. For long literals, it would be nice to allow some sort of grouping syntax; there's precedent in other languages for using '_' for this. Indeed, I used it above -- and have designed a language that used it, perhaps not coincidentally. And there's no good reason not to allow '_' in integer constants in general. Been there, done that! -- Spicy Hedgehog Scoring, bah. If I want scoring I'll go play /Age of Steam/. May 8 '07 #6

 P: n/a Keith Thompson wrote: Chris Dollin se*****@gmail.com wrote: >>Addition to that question :int a = 0xef;instead of this, can we assign the direct binary value 1110 1111to 'a' ??? No. Why do you want to?[`int a = 0xef;` /does/ initialise-with-the-binary-value-1110_1111 `a`.] The same argument could be used against hexadecimal literals; after all int a = 0xef; is the same as int a = 239; That is, as you know, not a valid analogy. In both hex and octal representations, each digit corresponds to a particular bit pattern. This is not true for decimal representations. But you know this, so why are you presenting what you know to be a specious and fallacious supposed analogy? May 8 '07 #7

 P: n/a Martin Ambuhl Chris Dollin >se*****@gmail.com wrote:Addition to that question :int a = 0xef;instead of this, can we assign the direct binary value 1110 1111to 'a' ???No. Why do you want to?[`int a = 0xef;` /does/ initialise-with-the-binary-value-1110_1111 `a`.] The same argument could be used against hexadecimal literals; afterall int a = 0xef;is the same as int a = 239; That is, as you know, not a valid analogy. In both hex and octal representations, each digit corresponds to a particular bit pattern. This is not true for decimal representations. But you know this, so why are you presenting what you know to be a specious and fallacious supposed analogy? Yes, I knew that, as I specifically acknowledged in the text that you snipped. Suppose, hypothetically, that C didn't have hexadecimal or octal constants, just decimal. I strongly suspect that the same argument *would* be used against adding them. After all, the hexadecimal value 0xef *could* be represented as (14<<4) + 15 or just 239 Hexadecimal constants are a considerable convenience, and I'm *not* seriously arguing that they're not a good idea. Binary constants would also be a convenience, and IMHO they'd be a good idea for the same reason (apart from the difficulty that goes with adding any new feature to the language). -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" May 8 '07 #8

 P: n/a Richard Heathfield C does not provide support for numeric literals expressed in binary representation. Nevertheless, it is possible to hack something up. Probably the best way is something simple such as: int a = strtoul("11101111", NULL, 2); Which isn't a constant expression. but you could always do something like: #define BINARY_8 ((((((((0 #define O )*2 #define l )*2+1 int a = BINARY_8 l l l O l l l l ; I prefer Tom Torf's style... #include #define B8(x) ((int) B8_(0x##x)) #define B8_(x) \ ( ((x) & 0xF0000000) >( 28 - 7 ) \ | ((x) & 0x0F000000) >( 24 - 6 ) \ | ((x) & 0x00F00000) >( 20 - 5 ) \ | ((x) & 0x000F0000) >( 16 - 4 ) \ | ((x) & 0x0000F000) >( 12 - 3 ) \ | ((x) & 0x00000F00) >( 8 - 2 ) \ | ((x) & 0x000000F0) >( 4 - 1 ) \ | ((x) & 0x0000000F) >( 0 - 0 ) ) #if INT_MAX < 65535 #define B16(h,l) ( ((unsigned) B8(h) << 8) \ | ((unsigned) B8(l) ) ) #else #define B16(h,l) ( (B8(h) << 8) | B8(l) ) #endif #include int main(void) { printf("0x%02X\n", (unsigned) B8(00000000)); printf("0x%02X\n", (unsigned) B8(00000001)); printf("0x%02X\n", (unsigned) B8(01010101)); printf("0x%02X\n", (unsigned) B8(10000000)); printf("0x%02X\n", (unsigned) B8(10101010)); printf("0x%02X\n", (unsigned) B8(11101111)); printf("0x%02X\n", (unsigned) B8(11111110)); printf("0x%02X\n", (unsigned) B8(11111111)); printf("0x%04X\n", (unsigned) B16(11011110,10101101)); return 0; } May 9 '07 #10

 P: n/a On May 8, 2:57 pm, "seth...@gmail.com"

 P: n/a Vinoj _asm MOV Dword Ptr a, 01111b vinoj.c:6: error: `_asm' undeclared (first use in this function) vinoj.c:6: error: (Each undeclared identifier is reported only once vinoj.c:6: error: for each function it appears in.) vinoj.c:6: error: parse error before "MOV" Ball tearer. -- Peter May 9 '07 #12

 P: n/a Peter Nilsson said: Richard Heathfield >C does not provide support for numeric literals expressed in binaryrepresentation. Nevertheless, it is possible to hack something up.Probably the best way is something simple such as:int a = strtoul("11101111", NULL, 2); Which isn't a constant expression. ....the only downside of which is that you can't use it to initialise objects with static storage duration, and that's no great loss. >but you could always do something like:#define BINARY_8 ((((((((0#define O )*2#define l )*2+1int a = BINARY_8 l l l O l l l l ; I prefer Tom Torf's style... Very neat. Harder to remember correctly in a pinch, though. :-) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. May 9 '07 #13

 P: n/a Doug said: c.l.c. is a complete bloody joke. Feel free to improve it. It'd be interesting to see how you go about this Sisyphean task, but unfortunately the most likely affect of any serious attempt will be that readers of the group will apply the same labels to you that you currently apply to others here. You can't legislate improvements to clc. Nobody here can do that. The best you are likely to achieve is to improve your own view of it, by judicious use of killfiles. The opportunity for me to give in to this temptation arose quite recently, when someone kindly posted (very OT!) details of how to tickle KNode in the proper way, and I took advantage of that opportunity. I can report that it was mostly a successful experiment, and comp.lang.c is a much more pleasant place nowadays - for me, anyway. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. May 9 '07 #14

 P: n/a On May 9, 11:14 am, Peter Nilsson _asm MOV Dword Ptr a, 01111b vinoj.c:6: error: `_asm' undeclared (first use in this function) vinoj.c:6: error: (Each undeclared identifier is reported only once vinoj.c:6: error: for each function it appears in.) vinoj.c:6: error: parse error before "MOV" Ball tearer. -- Peter I mean on the VC6 or later compilers. Thanks, Regards, Vinoj May 9 '07 #15

 P: n/a Doug

 P: n/a Vinoj wrote: Peter Nilsson Vinoj > >>I hope that this will be a good sample on the Intel platform. >> _asm MOV Dword Ptr a, 01111b vinoj.c:6: error: `_asm' undeclared (first use in this function) vinoj.c:6: error: (Each undeclared identifier is reported only once vinoj.c:6: error: for each function it appears in.) vinoj.c:6: error: parse error before "MOV"Ball tearer. I mean on the VC6 or later compilers. gcc 4.1 is much later. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com May 9 '07 #17

 P: n/a Vinoj wrote: "seth...@gmail.com" >int a = 0xef;instead of this, can we assign the direct binary value 1110 1111to 'a' ??? I hope that this will be a good sample on the Intel platform. int main(int argc, char* argv[]) { printf("Hello World!\n"); int a=0; _asm MOV Dword Ptr a, 01111b return 0; } Terrible. _asm is undefined. 01111b is undefined. int a=0 is illegal outside of C99. However you can use octal if you wish, or misunderstand hex: a = 0357; -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com May 9 '07 #18

 P: n/a Vinoj wrote, On 09/05/07 07:50: On May 9, 11:14 am, Peter Nilsson Vinoj >"seth...@gmail.com" >> _asm MOV Dword Ptr a, 01111b vinoj.c:6: error: `_asm' undeclared (first use in this function) vinoj.c:6: error: (Each undeclared identifier is reported only once vinoj.c:6: error: for each function it appears in.) vinoj.c:6: error: parse error before "MOV"Ball tearer.--Peter I mean on the VC6 or later compilers. I have a compiler later than VC6 that it won't work on. I even have a version of it for Windows! In any case, your solution is not C but an implementation specific extension that if it works will as a side effect reduce the chances for the optimiser to do its job. So I would strongly recommend against using it even if restricted to implementations where it would work. -- Flash Gordon May 9 '07 #19

 P: n/a On 9 May, 08:39, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote: Doug

### This discussion thread is closed

Replies have been disabled for this discussion.