EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Simple C question...entering binary

Started by Thomas Magma January 18, 2008
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.
David Brown escribi&#4294967295;:
> 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.

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
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?
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
>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

The 2024 Embedded Online Conference