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

What does the " ^ " character signify in C??

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



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

What does the " ^ " character signify in C??
PostPosted: Wed Nov 02, 2005 10:32 am     Reply with quote

I have far from seen it all, as this question will prove beyond a doubt. ;)

In the following code segment from http://www.ccsinfo.com/forum/viewtopic.php?p=53837#53837 (ckielstra's thread), the ^-sign is used to do "something" but I've never seen it before and I cant find it in the documentation or with google.. I guess that is more due to my searches beeing bad than anything else...

Anywas, if anyone could explain a couple of the lines using the ^operator I would be very happy:
Code:

int16 crc_1021(int16 old_crc, int8 data)
{
  int16 crc;
  int16 x;

  x = make8(old_crc,1) ^ data;  //x = ((old_crc>>8) ^ data) & 0xff;
  x ^= x>>4;

  crc = (old_crc << 8) ^ (x << 12) ^ (x <<5) ^ x;

  crc &= 0xffff;

  return crc;
}

_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
Ttelmah
Guest







PostPosted: Wed Nov 02, 2005 10:40 am     Reply with quote

Bitwise exclusive OR.

Best Wishes
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

PostPosted: Wed Nov 02, 2005 10:47 am     Reply with quote

aha!

Good. Need to write an implementation for this in LabVIEW as well, for the ground control, and wasnt sure how to translate that! :D

Thanks!
_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Nov 02, 2005 2:12 pm     Reply with quote

Dargar,
Just for reference it is documented in the October version of the CCS Manual on PDF page 82.
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

PostPosted: Wed Nov 02, 2005 2:42 pm     Reply with quote

Thank you, always nice to know!
_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 5:09 am     Reply with quote

[edit]
after reading in the manual, I can not find any specific information for the << and >> operators, and they are not the same as the shift_left and shift_right are they? Sorry if I missed it, but I did search through the manual using pdf.
[/edit]
humm. would have been nice with some more explicit details on the ">>" and "<<" operators. do they shift in zeroes or wrap around or ones?

After answering the above, bear with me as I try to "compile" the c-code above into words to check that I understand whats going on:
(see comments for each line)

Code:

int16 crc_1021(int16 old_crc, int8 data)
{
  int16 crc;
  int16 x;

  x = make8(old_crc,1) ^ data; //x = msb of old_crc
  x ^= x>>4;  //x is xor'ed with y, where y = x shifted 4 bits to the right

  crc = (old_crc << 8) ^ (x << 12) ^ (x <<5) ^ x;
// "shift old_crc 8 steps to left" XOR'ed with "x shifted 12 steps to left"
// XOR'ed with "x shifted 5 steps to left" XOR'ed with x... does the shifts shift in 0's 1's or wraps?

  crc &= 0xffff;

  return crc;
}

_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace


Last edited by Dargar on Fri Nov 04, 2005 5:12 am; edited 1 time in total
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Fri Nov 04, 2005 5:12 am     Reply with quote

they shift in zero's
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Nov 04, 2005 6:06 am     Reply with quote

Standard C stuff. Rotate Left and Right.
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

Ok, got all of it. but still trying to understand it
PostPosted: Fri Nov 04, 2005 11:01 am     Reply with quote

[edit]
for some stupid reason, I left all my CCS development kits in the states when I went to Norway for a 3 month stay, so I cant run the CCS code and print to a terminal like I would normally do!!
[/edit]

I got how it works and have reproduced it in LabVIEW, I think.
To validate, I took the PIC c-code and adapted it to run as a console application in Microsoft Visual Studio C++ (no flaming pls, I also own a Borland Builder C/C++ lisence! ;) ).

Anyway, my question has to do with what the CCS "Make8()" is equivalent to.

According to the CCS documentation:
Quote:

Extracts the byte at offset from var. Same as: i8 = (((var >> (offset*8)) & 0xff) except it is done with a single byte move.

But... what is the purpose of the last "& 0xFF"? and'ing with 1's will leave 0's where there was 0's before, and 1's where there where 1's before, e.g. it wont change anything? (and the shift has already zeroed the upper parts of the 16 or 32 bit value.)

I tried to remove this in the c++ app, without any changes to the outputs!
(var's in c++ defined as such:
unsigned __int16 old_crc;
unsigned __int16 crc;
unsigned __int16 x;
unsigned __int8 data;
)

Also, is it recommended to start with "old_crc" as 0xFFFF or 0x0000 when running the first byte through?

[edit 2]
And it's the same with the very last instruction, crc &=0xFFFF; Why on earth waste cycles on this? Does it have to do with how CCS compiles or have I totally missed something here?
[/edit 2]
_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
Ttelmah
Guest







PostPosted: Fri Nov 04, 2005 11:12 am     Reply with quote

The 'key' to this, lies in C.
A >> operator, _does not_ guarantee to fill with zeroes. It _will_, if the value is 'unsigned'. However for a 'signed' value, what happens is left to the processor. So in K&R, it says:
"Right shifting a signed quantity will fill with sign bits ("arithmetic shift") on some machines, and with 0 bits ("logical shift") on others".
Hence the generic method to obtain the top byte of a two byte number (which will work on either machine type), is to shift, then mask.
CCS, were posting the precise 'equivalent' function, that will work with processors that behave in either way.

Best Wishes
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 11:25 am     Reply with quote

aha!

That explains that bit.

I guess it actually explains why the
Code:

crc &= 0xFFFF;

as well, only, in this case we actually have total control over the processors, so my question then is: For Microchip PIC targets compiled with the CCS compiler, is it strictly necessary or can this be dropped (foregoing "ANSI C") to speed up the code with a couple of cycles?

Actually, a better way to answer me on this one could be to explain the steps I can take in CCS to compare the code generated by the two different approaches and how the thought process goes when determining if it is necessary.

(From the previous post by Ttelmah, it should NOT be necessary since integers in CCS by default is unsigned?)
_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 11:46 am     Reply with quote

If crc is an int16 and you were to & with 0xFFFF you would see that no code is compiled for the operation.
Dargar



Joined: 12 Dec 2003
Posts: 25

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 11:52 am     Reply with quote

Neutone wrote:
If crc is an int16 and you were to & with 0xFFFF you would see that no code is compiled for the operation.


to bad, I was starting to think I had actually managed to squeese a few more cycles out of it! :D

Thanks!
_________________
Owner of PCWH v. 3.224
Consider myself beginer\intermediate as far as CCS and Microchip PIC's goes.
Bachelor in Space Engineering and Undergraduate Masters in Space Engineering
soon to be undergrad Aerospace
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