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

#bit

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



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

#bit
PostPosted: Sat Dec 10, 2011 2:38 am     Reply with quote

Hello. I'm amateur in writing codes at CCS. Can anyone explain me in details, the function of: #bit. Excuse my poor english :D, thx!
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sat Dec 10, 2011 3:40 am     Reply with quote

It all ties up with #byte....

OK. If you declare a variable 'fred' say, then this is a name for a piece of memory, which you can store number in, without worrying 'where' the numbers are actually stored.
#byte allows you to locate such a variable _at_ a specific location. So you can use:
Code:


int8 fred;
#byte fred = 0x60


Which then locates 'fred' at memory location 0x60. You can do this with any normal variable (char, int, int16, float etc...).
However there is a shortcut, that if you just use:

#byte fred = 0x60

Then the compiler will automatically _create_ an int8 variable and put it at the specified location.
There is also a second similar shortcut, with:

#word tom = 0x60

Which will create an int16 variable 'tom' and put this at memory location 0x60.

So these functions allow you to put a variable that is one byte or larger in size at a specific location.

However what happens if you want to access just on bit at a specific location?. Now, hopefully you understand, that a 'bit', is a 'binary digit'. A single '1' or '0'. The smallest element of data in a computer.
You could (for instance) set the bottom bit of a variable with:

Code:

    int8 fred;
    fred=32;

    //now set the bottom bit with:
    fred |=1;
    //fred now is 33


This "OR's" the variable with 1, so in the binary representation:

00100000 - fred
00000001

00100001 - result of the two values ORed together.

The bit in the result is a '1' if it is '1' in fred _or_ the second value.

Similarly you can turn off a bit using 'AND', toggle a bit with 'XOR' etc..

However in each case, you have to read the whole byte, perform the arithmetic, and write the byte back. Similarly to test a single bit you would have to perfrom an 'AND', and then test the result. Lots of work.

Key though is that the PIC, has instructions to access the individual bits in a register. Offering the ability to perform all these operations in single instructions.

#bit allows you to use these, and treat a single bit in a memory location as if it was a register in it's own right.

So:
Code:

    int8 fred;
#bit fred_low_bit = fred.0 //The bottom bit of fred

    fred=32;

    //now set the bottom bit with:
    fred_low_bit=1;
    //fred now is 33

So this creates a variable called 'fred_low_bit', which directly accesses _just_ the bottom bit of 'fred'. You can read it, write it, test it etc...

Biggest use, is where a variable has been moved with #byte to correspond to a physical register in the chip, where individual bits of registers give specific information, or control particular functions. Using #bit you can directly access these bits.

Best Wishes
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 2:08 am     Reply with quote

Thank you for exhaustive information! It was very usefull for me.
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