Simple C question...entering binary

Started by Thomas Magma January 18, 2008
Quick question.

How do you enter a literal in binary? That is, the binary equivalent of:

x = 0xFFFF //hex entry

Thank,
Thomas


Thomas Magma wrote:
> Quick question. > > How do you enter a literal in binary? That is, the binary equivalent of: > > x = 0xFFFF //hex entry
x = 0xFFFF; I have seen some compilers with non-standard extensions that allow the similar form x = 0b11111111; but that IS non-standard. Probably if one really, really wanted to do that, the source could be run through a Perl or awk filter before handing it off to the pre-processor. Also once saw (and was amazed by) a page of pre-processor macros that took a "binary" argument and emitted the equivalent hex form. -- Rich Webb Norfolk, VA
>
> x = 0b11111111; >
The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the first time and I like to set my TRIS register values in binary.) Thanks Rich.
Thomas Magma wrote:
> How do you enter a literal in binary? That is, the binary equivalent of: > > x = 0xFFFF //hex entry
You translate it to hex and use that :-) Or, you define mnemonic names for the actual bits you're using and use these in an expression, as in GCTL = TXEN | RXEN | STOP_BIT | WORD_SIZE_8 instead of GCTL = 0b00110101; (register names have been made up). That aside, the preprocessor magic version is a variation of #define BINARY_OCTET(x) \ (((0##x & 01)) | \ ((0##x & 010) >> 2) | \ ((0##x & 0100) >> 4) | \ ((0##x & 01000) >> 6) | \ ((0##x & 010000) >> 8) | \ ((0##x & 0100000) >> 10) | \ ((0##x & 01000000) >> 12) | \ ((0##x & 010000000) >> 14)) int main() { printf("%x\n", BINARY_OCTET(10100100)); return 0; } It's fun for its hack value, but I wouldn't want it in production code. Stefan
"Thomas Magma"  wrote:
>How do you enter a literal in binary? That is, the binary equivalent of: >x = 0xFFFF //hex entry
There is a clever set of macros from Tom Torfs to do this. See: http://www.velocityreviews.com/forums/t317511-binary-constant-macros.html Roberto Waltman [ Please reply to the group, return address is invalid ]
Thomas Magma wrote:
> > How do you enter a literal in binary? That is, the binary > equivalent of: x = 0xFFFF //hex entry
You can enter the value in hex, octal, or decimal. For example: unsigned int x; .... x = 0xffff; /* or */ x = 0177777; /* or */ x = 65535U; /* or */ x = 0170000 + 0xf00 + 255U; -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
On Fri, 18 Jan 2008 21:08:21 +0100, Stefan Reuther
<stefan.news@arcor.de> wrote:

>That aside, the preprocessor magic version is a variation of > #define BINARY_OCTET(x) \ > (((0##x & 01)) | \ > ((0##x & 010) >> 2) | \ > ((0##x & 0100) >> 4) | \ > ((0##x & 01000) >> 6) | \ > ((0##x & 010000) >> 8) | \ > ((0##x & 0100000) >> 10) | \ > ((0##x & 01000000) >> 12) | \ > ((0##x & 010000000) >> 14)) > > int main() { > printf("%x\n", BINARY_OCTET(10100100)); > return 0; > } >It's fun for its hack value, but I wouldn't want it in production code.
Why not. I'd say a decent compiler will just calculate it at compile time. -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
"Thomas Magma" <somewhere@overtherainbow.com> skrev i meddelandet 
news:Yw7kj.100328$EA5.10420@pd7urf2no...
> > >> x = 0b11111111; >> > > The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the > first time and I like to set my TRIS register values in binary.) > > Thanks Rich. > >
And here is the ANSI C compatible way. #define BIT(n) (1 << n) #define BIT0 BIT(0) #define BIT1 BIT(1) #define BIT2 BIT(2) #define BIT3 BIT(3) #define BIT4 BIT(4) #define BIT5 BIT(5) #define BIT6 BIT(6) #define BIT7 BIT(7) TRIS = BIT7 | BIT3 | BIT0; -- Best Regards, Ulf Samuelsson This is intended to be my personal opinion which may, or may not be shared by my employer Atmel Nordic AB
In article <47914AAB.E10DFAB8@yahoo.com>,
CBFalconer  <cbfalconer@maineline.net> wrote:
>Thomas Magma wrote: >> >> How do you enter a literal in binary? That is, the binary >> equivalent of: x = 0xFFFF //hex entry > >You can enter the value in hex, octal, or decimal. For example: > > unsigned int x; > .... > x = 0xffff; >/* or */ x = 0177777; >/* or */ x = 65535U; >/* or */ x = 0170000 + 0xf00 + 255U;
And the most convenient of all x = -1; (For those not in the know, the definition of unsigned explicitly makes this works. There may be an additional advantage that it ports over to 64 bits more easily.) And of course: x = ~0;
> >-- > [mail]: Chuck F (cbfalconer at maineline dot net) > [page]: <http://cbfalconer.home.att.net> > Try the download section. > > > >-- >Posted via a free Usenet account from http://www.teranews.com >
-- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- like all pyramid schemes -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
Albert van der Horst wrote:
> CBFalconer <cbfalconer@maineline.net> wrote: >> Thomas Magma wrote: >>> >>> How do you enter a literal in binary? That is, the binary >>> equivalent of: x = 0xFFFF //hex entry >> >> You can enter the value in hex, octal, or decimal. For example: >> >> unsigned int x; >> .... >> x = 0xffff; >> /* or */ x = 0177777; >> /* or */ x = 65535U; >> /* or */ x = 0170000 + 0xf00 + 255U; > > And the most convenient of all > x = -1; > > (For those not in the know, the definition of unsigned > explicitly makes this works. There may be an additional > advantage that it ports over to 64 bits more easily.)
Depends on the objective. x = -1 will create the maximum unsigned value possible (which might be 0xffffffff on some machines). The sequences I showed will create 0xffff, regardless of the size of the int. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com
>Thomas Magma wrote:
>> How do you enter a literal in binary? That is, the binary equivalent
of:
>> >> x = 0xFFFF //hex entry > >You translate it to hex and use that :-) Or, you define mnemonic names >for the actual bits you're using and use these in an expression, as in > GCTL = TXEN | RXEN | STOP_BIT | WORD_SIZE_8 >instead of > GCTL = 0b00110101; >(register names have been made up). > >That aside, the preprocessor magic version is a variation of > #define BINARY_OCTET(x) \ > (((0##x & 01)) | \ > ((0##x & 010) >> 2) | \ > ((0##x & 0100) >> 4) | \ > ((0##x & 01000) >> 6) | \ > ((0##x & 010000) >> 8) | \ > ((0##x & 0100000) >> 10) | \ > ((0##x & 01000000) >> 12) | \ > ((0##x & 010000000) >> 14)) > > int main() { > printf("%x\n", BINARY_OCTET(10100100)); > return 0; > } >It's fun for its hack value, but I wouldn't want it in production code. > > > Stefan > >
In my opinion, the interesting downside of mnemonics is that you have to get it right twice; right with the definition of the mnemonic (in your .H file or wherever), and right in its usage. Also, it isn't as clear when you read the code. I'd rather see a hex literal than a mnemonic; at least I can directly reference the register bit settings from a hardware device data sheet rather than finding the mnemonic definition in a .H file, then doing the reference. Jeff http://www.e2atechnology.com
On Wed, 16 Jul 2008 08:59:34 PST,
mojaveg@mojaveg.lsan.mdsg-pacwest.com (Everett M. Greene) wrote:

>Walter Banks <walter@bytecraft.com> writes: >> David Brown wrote: >> >> > > Reading binary numbers is easier when the language lets you separate the >> > > binary digits into logical groups, for example with underscores as in >> > > the Ada form: >> > > >> > > 2#10_10110_001000_11111_1_0000000010000# >> > > >> > > which is the 32-bit encoding of the SPARC instruction "addx r31,16,r22", >> > > grouped according to the logical fields of the encoding. The "_" has no >> > > numerical meaning, so the above is the same as: >> > > >> > > 2#10101100010001111110000000010000# >> > > >> > > Perhaps the C compilers that support the 0b... notation should allow >> > > some form of grouping like this. >> > > >> > >> > The Ada-style "_" spacer in numbers (for hex and decimal as well as bin) >> > would be a huge improvement to readability for longer strings. It >> > should also appeal to the "hosted" folk as well as the "non-hosted" >> > folk, so perhaps Walter might have some luck suggesting it to the powers >> > that be in the C world. >> >> I like the spacer syntax. There is at least one C compiler that supports >> spacers (not ours) I don't think that spacer syntax is likely to be introduced >> into C anytime soon. C has never had it and there are some things that >> need to be resolved long before this becomes a pressing issue. >> >> In our case we added 0B... , 0b.... with the same syntax as hex support. >> I recognize the argument for hex support only and legacy support for >> octal notation. My customers are almost all non-hosted embedded >> systems folks and we have supported their needs with the extension. > >I would think the spacer idea could be adopted faily readily >since it's upward (forward?) compatible and is nearly trivial >to implement. > >Now, for a real miracle, get them to revise some of the string >functions to return something useful. Who has any need for the >first arg to strcpy() as the return value?
Slow imagination? This popped immediately to mind (I'm sure a compiler writer will provide better.) Consider the ?: syntax for a moment and how it _might_ be used. Jon
Walter Banks <walter@bytecraft.com> writes:
> David Brown wrote: > > > > Reading binary numbers is easier when the language lets you separate the > > > binary digits into logical groups, for example with underscores as in > > > the Ada form: > > > > > > 2#10_10110_001000_11111_1_0000000010000# > > > > > > which is the 32-bit encoding of the SPARC instruction "addx r31,16,r22", > > > grouped according to the logical fields of the encoding. The "_" has no > > > numerical meaning, so the above is the same as: > > > > > > 2#10101100010001111110000000010000# > > > > > > Perhaps the C compilers that support the 0b... notation should allow > > > some form of grouping like this. > > > > > > > The Ada-style "_" spacer in numbers (for hex and decimal as well as bin) > > would be a huge improvement to readability for longer strings. It > > should also appeal to the "hosted" folk as well as the "non-hosted" > > folk, so perhaps Walter might have some luck suggesting it to the powers > > that be in the C world. > > I like the spacer syntax. There is at least one C compiler that supports > spacers (not ours) I don't think that spacer syntax is likely to be introduced > into C anytime soon. C has never had it and there are some things that > need to be resolved long before this becomes a pressing issue. > > In our case we added 0B... , 0b.... with the same syntax as hex support. > I recognize the argument for hex support only and legacy support for > octal notation. My customers are almost all non-hosted embedded > systems folks and we have supported their needs with the extension.
I would think the spacer idea could be adopted faily readily since it's upward (forward?) compatible and is nearly trivial to implement. Now, for a real miracle, get them to revise some of the string functions to return something useful. Who has any need for the first arg to strcpy() as the return value?

David Brown wrote:

> > Reading binary numbers is easier when the language lets you separate the > > binary digits into logical groups, for example with underscores as in > > the Ada form: > > > > 2#10_10110_001000_11111_1_0000000010000# > > > > which is the 32-bit encoding of the SPARC instruction "addx r31,16,r22", > > grouped according to the logical fields of the encoding. The "_" has no > > numerical meaning, so the above is the same as: > > > > 2#10101100010001111110000000010000# > > > > Perhaps the C compilers that support the 0b... notation should allow > > some form of grouping like this. > > > > The Ada-style "_" spacer in numbers (for hex and decimal as well as bin) > would be a huge improvement to readability for longer strings. It > should also appeal to the "hosted" folk as well as the "non-hosted" > folk, so perhaps Walter might have some luck suggesting it to the powers > that be in the C world.
I like the spacer syntax. There is at least one C compiler that supports spacers (not ours) I don't think that spacer syntax is likely to be introduced into C anytime soon. C has never had it and there are some things that need to be resolved long before this becomes a pressing issue. In our case we added 0B... , 0b.... with the same syntax as hex support. I recognize the argument for hex support only and legacy support for octal notation. My customers are almost all non-hosted embedded systems folks and we have supported their needs with the extension. Walter Banks Byte Craft Limited
David Brown escribió:
> Niklas Holsti wrote: >> Robert Adsett wrote: >>> In article <487CBD04.6B50B16E@bytecraft.com>, Walter Banks says... >>> >>>> >>>> David Brown wrote: >>>> >>>> >>>>> Walter Banks wrote: >>>>> >>>>>> Most of the embedded people did something similar >>>>>> >>>>>> #define b00000000 0 >>>>>> #define b00000001 1 >>>>>> . . . >>>>>> >>>>>> before 0b.... >>>>>> >>>>>> was implemented as an extension in most compilers including ours. >>>>>> >>>>> >>>>> I'd guess that "most of the embedded people" used hex or some sort >>>>> of (1 >>>>> << n) notation. But it's nice having 0b numbers available - is it >>>>> common on other compilers yet? (gcc has it from 4.3 onwards, with >>>>> avr-gcc supporting it from much earlier.) >>>> >>>> Many embedded compilers now to support 0b.... I don't have a list of >>>> who >>>> does support the binary syntax. I raised the issue at WG-14 one >>>> time and >>>> support was divided between hosted vs non-hosted experience. The >>>> hosted folks suggested 0x... was all that was needed. >>> >>> >>> I tend to agree with them. I find reading hex (or x << y) a lot >>> easier than counting bits in a binary number. >> >> Reading binary numbers is easier when the language lets you separate >> the binary digits into logical groups, for example with underscores as >> in the Ada form: >> >> 2#10_10110_001000_11111_1_0000000010000# >> >> which is the 32-bit encoding of the SPARC instruction "addx >> r31,16,r22", grouped according to the logical fields of the encoding. >> The "_" has no numerical meaning, so the above is the same as: >> >> 2#10101100010001111110000000010000# >> >> Perhaps the C compilers that support the 0b... notation should allow >> some form of grouping like this. >> > > The Ada-style "_" spacer in numbers (for hex and decimal as well as bin) > would be a huge improvement to readability for longer strings. It > should also appeal to the "hosted" folk as well as the "non-hosted" > folk, so perhaps Walter might have some luck suggesting it to the powers > that be in the C world.
I was used to PL/M's "$" null separator, which let me write VALUE = 0100$1100B; or something like that. This was circa 1988. When I switched to C, I was surprised to see no "B" nor "$" equivalent forms. I agree, though, that Ada's "_" is far superior.
Niklas Holsti wrote:
> Robert Adsett wrote: >> In article <487CBD04.6B50B16E@bytecraft.com>, Walter Banks says... >> >>> >>> David Brown wrote: >>> >>> >>>> Walter Banks wrote: >>>> >>>>> Most of the embedded people did something similar >>>>> >>>>> #define b00000000 0 >>>>> #define b00000001 1 >>>>> . . . >>>>> >>>>> before 0b.... >>>>> >>>>> was implemented as an extension in most compilers including ours. >>>>> >>>> >>>> I'd guess that "most of the embedded people" used hex or some sort >>>> of (1 >>>> << n) notation. But it's nice having 0b numbers available - is it >>>> common on other compilers yet? (gcc has it from 4.3 onwards, with >>>> avr-gcc supporting it from much earlier.) >>> >>> Many embedded compilers now to support 0b.... I don't have a list of who >>> does support the binary syntax. I raised the issue at WG-14 one >>> time and >>> support was divided between hosted vs non-hosted experience. The >>> hosted folks suggested 0x... was all that was needed. >> >> >> I tend to agree with them. I find reading hex (or x << y) a lot >> easier than counting bits in a binary number. > > Reading binary numbers is easier when the language lets you separate the > binary digits into logical groups, for example with underscores as in > the Ada form: > > 2#10_10110_001000_11111_1_0000000010000# > > which is the 32-bit encoding of the SPARC instruction "addx r31,16,r22", > grouped according to the logical fields of the encoding. The "_" has no > numerical meaning, so the above is the same as: > > 2#10101100010001111110000000010000# > > Perhaps the C compilers that support the 0b... notation should allow > some form of grouping like this. >
The Ada-style "_" spacer in numbers (for hex and decimal as well as bin) would be a huge improvement to readability for longer strings. It should also appeal to the "hosted" folk as well as the "non-hosted" folk, so perhaps Walter might have some luck suggesting it to the powers that be in the C world.
Robert Adsett wrote:
> In article <487CBD04.6B50B16E@bytecraft.com>, Walter Banks says... > >> >>David Brown wrote: >> >> >>>Walter Banks wrote: >>> >>>>Most of the embedded people did something similar >>>> >>>>#define b00000000 0 >>>>#define b00000001 1 >>>>. . . >>>> >>>>before 0b.... >>>> >>>>was implemented as an extension in most compilers including ours. >>>> >>> >>>I'd guess that "most of the embedded people" used hex or some sort of (1 >>><< n) notation. But it's nice having 0b numbers available - is it >>>common on other compilers yet? (gcc has it from 4.3 onwards, with >>>avr-gcc supporting it from much earlier.) >> >>Many embedded compilers now to support 0b.... I don't have a list of who >>does support the binary syntax. I raised the issue at WG-14 one time and >>support was divided between hosted vs non-hosted experience. The >>hosted folks suggested 0x... was all that was needed. > > > I tend to agree with them. I find reading hex (or x << y) a lot easier > than counting bits in a binary number.
Reading binary numbers is easier when the language lets you separate the binary digits into logical groups, for example with underscores as in the Ada form: 2#10_10110_001000_11111_1_0000000010000# which is the 32-bit encoding of the SPARC instruction "addx r31,16,r22", grouped according to the logical fields of the encoding. The "_" has no numerical meaning, so the above is the same as: 2#10101100010001111110000000010000# Perhaps the C compilers that support the 0b... notation should allow some form of grouping like this. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .
In article <487CBD04.6B50B16E@bytecraft.com>, Walter Banks says...
> > > David Brown wrote: > > > Walter Banks wrote: > > > Most of the embedded people did something similar > > > > > > #define b00000000 0 > > > #define b00000001 1 > > > . . . > > > > > > before 0b.... > > > > > > was implemented as an extension in most compilers including ours. > > > > > > > I'd guess that "most of the embedded people" used hex or some sort of (1 > > << n) notation. But it's nice having 0b numbers available - is it > > common on other compilers yet? (gcc has it from 4.3 onwards, with > > avr-gcc supporting it from much earlier.) > > Many embedded compilers now to support 0b.... I don't have a list of who > does support the binary syntax. I raised the issue at WG-14 one time and > support was divided between hosted vs non-hosted experience. The > hosted folks suggested 0x... was all that was needed.
I tend to agree with them. I find reading hex (or x << y) a lot easier than counting bits in a binary number. Robert ** Posted from http://www.teranews.com **

David Brown wrote:

> Walter Banks wrote: > > Most of the embedded people did something similar > > > > #define b00000000 0 > > #define b00000001 1 > > . . . > > > > before 0b.... > > > > was implemented as an extension in most compilers including ours. > > > > I'd guess that "most of the embedded people" used hex or some sort of (1 > << n) notation. But it's nice having 0b numbers available - is it > common on other compilers yet? (gcc has it from 4.3 onwards, with > avr-gcc supporting it from much earlier.)
Many embedded compilers now to support 0b.... I don't have a list of who does support the binary syntax. I raised the issue at WG-14 one time and support was divided between hosted vs non-hosted experience. The hosted folks suggested 0x... was all that was needed. w..
Walter Banks wrote:
> Most of the embedded people did something similar > > #define b00000000 0 > #define b00000001 1 > . . . > > before 0b.... > > was implemented as an extension in most compilers including ours. >
I'd guess that "most of the embedded people" used hex or some sort of (1 << n) notation. But it's nice having 0b numbers available - is it common on other compilers yet? (gcc has it from 4.3 onwards, with avr-gcc supporting it from much earlier.)