View previous topic :: View next topic |
Author |
Message |
matrixofdynamism
Joined: 06 Dec 2010 Posts: 25
|
How to write binary literal in CCS C? |
Posted: Sun Mar 20, 2016 1:32 pm |
|
|
Hexadecimal can be written by starting with 0x, what about binary literal? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Mar 20, 2016 1:37 pm |
|
|
0b
Standard C. |
|
|
matrixofdynamism
Joined: 06 Dec 2010 Posts: 25
|
|
Posted: Tue Mar 22, 2016 2:45 am |
|
|
I see.
This seems to be not part of the C/C++ standard:
According to Rationale for International Standard - Programming Languages C ยง6.4.4.1 Integer constants
A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.
In any case the compiler has support for this feature. Interesting. I guess that the _ character can be used to space the digits too. I shall verify that today. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Mar 22, 2016 3:18 am |
|
|
matrixofdynamism wrote: | I see.
This seems to be not part of the C/C++ standard
A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.
|
There is no (universal) "C/C++ standard". There are several different C standards and several more C++ ones. Things can be, and more importantly are, different from standard to standard. There is and can be no one size fits all, despite what the standardisers want us to believe.
That's especially true with embedded forms of C, which by their very nature, with their close relationship to hardware, are non-standard. There have to be extensions and deviances. For instance, the Harvard architecture of most PICs forces certain implementation compromises on to PIC Cs. Also, interrupts would be impossible to code for in all standard forms of C that I am aware of.
Binary literals do indeed appear in few C standards, but they are a common extension especially in embedded Cs. Contrary to that commentary (I do feel the standardisers may have been somewhat obtuse and in some cases overly PC/large machine focused, largely ignoring a main use of C, arguably the prime reason for its survival and longevity, i.e. embedded programming) binary literals do have utility in embedded applications.
They also have a problem: binary literals quickly get unreadable and therefore unmanageable. Eight bit literals are fine. Sixteen bitters are getting unwieldy. Thirty-two bit binary literals are, in my view, a nightmare. I accept the point that other representations, most notably hex (though way back, octal was more common for various historical reasons: its all but forgotten now) are more universally useful. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Tue Mar 22, 2016 4:42 am |
|
|
and (of course), binary and octal literals were part of K&R. The original 'C' standard, which is what CCS follows far more exactly than anything later.
However most modern users, would probably go hex. It is far easier as values get larger. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 22, 2016 2:13 pm |
|
|
Never did an int32 binary but......
My standard template for binary literals which i admit i use because they are better self documenting when writing drivers:
SO MUCH EASIER TO VISUALIZE THAN values like 0x7C5A
// -----> 76543210
wht8= 0b00000000;
// -------------> FEDCBA9876543210
whatever16= 0b0000000000000000; |
|
|
|