CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to write binary literal in CCS C?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
matrixofdynamism



Joined: 06 Dec 2010
Posts: 25

View user's profile Send private message

How to write binary literal in CCS C?
PostPosted: Sun Mar 20, 2016 1:32 pm     Reply with quote

Hexadecimal can be written by starting with 0x, what about binary literal?
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Sun Mar 20, 2016 1:37 pm     Reply with quote

0b

Standard C.
matrixofdynamism



Joined: 06 Dec 2010
Posts: 25

View user's profile Send private message

PostPosted: Tue Mar 22, 2016 2:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 22, 2016 3:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 22, 2016 4:42 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Mar 22, 2016 2:13 pm     Reply with quote

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;
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group