By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 709 Online
Bytes IT Community
+ 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
Share this Question
Share on Google+
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 <ch**********@hp.comwrites:
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`.]
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 <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 8 '07 #5

P: n/a
Keith Thompson wrote:
Chris Dollin <ch**********@hp.comwrites:
>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`.]

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 <ch**********@hp.comwrites:
>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`.]

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 <ma*****@earthlink.netwrites:
Keith Thompson wrote:
>Chris Dollin <ch**********@hp.comwrites:
>>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`.]
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?
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 <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 8 '07 #8

P: n/a
c.l.c. is a complete bloody joke.

With 8 posts and such an innocent title, I was hoping this might be a
piss-free thread. Obviously not. Look at you all, you're like bloody
children.

Given the 'helpful advice' in this thread, the OP probably won't be
back here, at least not with the same name. Look through past threads
over the last few months; first time posters don't tend to come back.

There are about 9 or 10 frequent posters to this group. One or two
(including KT) I respect; their replies are always helpful and usually
polite. The rest of you are like infants squabbling for attention and
validation. You don't try to help, you're just trying to compare
ballsack sizes.

For example, the first mistake usually results in torrents of
'undefined behaviour', 'you fool', etc., and no further reading. "Fix
void main() before we help!" Snide comment obligatory. It would have
been so easy to go a little further and point out a few other
problems, but helping the OP isn't generally the goal for most of
you. The real goal is showing off your ballsack size. I love the 'i
know the answer but I'm not telling you' posts; these are particularly
'helpful'.

You're obviously all very intelligent and all great at C. But in the
real world, that's only part of the deal; interacting with people is
also important. I'm sure you all have very important jobs. Maybe you
just vent on usenet what you can't get away with at work.

Sorry for the rant. I'm usually restrained and just giggle my way
through your banter - I use it as wind down time after work (probably
much in the same way as you do, except I don't find the need to
belittle others). I had a few too many at the pub tonight. Hey, it's
my birthday in a couple of days, and I'll have to drop the leading 2.
Leave it out.

May 8 '07 #9

P: n/a
Richard Heathfield <r...@see.sig.invalidwrote:
>
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 <limits.h>

#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 <stdio.h>

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" <seth...@gmail.comwrote:
Addition to that question :

int a = 0xef;

instead of this, can we assign the direct binary value 1110 1111
to 'a' ???

-Sethu
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;
}

Thanks, Regards,
Vinoj

May 9 '07 #11

P: n/a
Vinoj <vinoj.kuma...@gmail.comwrote:
"seth...@gmail.com" <seth...@gmail.comwrote:
int a = 0xef;
instead of this, can we assign the direct binary
value 1110 1111 to 'a' ???

I hope that this will be a good sample on the Intel platform.
<snip>
_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 <r...@see.sig.invalidwrote:
>>
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.
....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+1

int 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 <a...@acay.com.auwrote:
Vinoj <vinoj.kuma...@gmail.comwrote:
"seth...@gmail.com" <seth...@gmail.comwrote:
int a = 0xef;
instead of this, can we assign the direct binary
value 1110 1111 to 'a' ???
I hope that this will be a good sample on the Intel platform.
<snip>
_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 <Do*********@googlemail.comwrote:
c.l.c. is a complete bloody joke.
Don't let the door hit you on the way out.

Alternatively, do something about it. Complaining about how bad a
newsgroup is never improved one. Posting better on-topic content does.

Richard
May 9 '07 #16

P: n/a
Vinoj wrote:
Peter Nilsson <a...@acay.com.auwrote:
>Vinoj <vinoj.kuma...@gmail.comwrote:
.... snip ...
>>
>>I hope that this will be a good sample on the Intel platform.
<snip>
>> _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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
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" <seth...@gmail.comwrote:
>>
int a = 0xef;

instead of this, can we assign the direct binary value 1110 1111
to '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;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
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 <a...@acay.com.auwrote:
>Vinoj <vinoj.kuma...@gmail.comwrote:
>>"seth...@gmail.com" <seth...@gmail.comwrote:
int a = 0xef;
instead of this, can we assign the direct binary
value 1110 1111 to 'a' ???
I hope that this will be a good sample on the Intel platform.
<snip>
>> _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 <DougTheS...@googlemail.comwrote:
c.l.c. is a complete bloody joke.

Don't let the door hit you on the way out.

Alternatively, do something about it. Complaining about how bad a
newsgroup is never improved one. Posting better on-topic content does.

Richard
Indeed. I awoke this morning with a shitty hangover *and* the feeling
I'd been a twat (in general, not an uncommon experience for me) on
this newsgroup. I had to look it up to check, due to memory loss/a
double free() I appear to have made last night. I shouldn't have
posted, my apologies, etc. I'm going to look into adding a sobriety
test into my posting software. I also realise this post isn't adding
to the on-topic count.

I generally don't post in this group because there are regulars here
who cover the material far better than I ever could. My problem is in
the way that knowledge is conveyed. I just don't see the need for the
condescending tones, the pedantry, the basic lack of respect unless
you're 'one of the regulars', etc. I guess something tipped me last
night (but hell, who's kidding, I was so out of it, it could have been
my kitten).

I realise this is probably *my* problem and my skin isn't thick
enough. But thank the gods I found this newsgroup *after* I'd learned
to program (the internet wasn't around when that happened). I'm no
expert and I always learn useful things here, but if I'd had to use
this group as a learning tool then there's a pretty good chance I
would have given up a long time ago and taken up knitting.

Doug

May 9 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.