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

Compare two bytes with AND

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



Joined: 25 Nov 2004
Posts: 30
Location: Germany

View user's profile Send private message

Compare two bytes with AND
PostPosted: Mon Jun 06, 2005 6:30 am     Reply with quote

Hello guys,

I want to compare two bytes with each other to know, if they have one of them has a 1 or a 0 in a special bit.

By can-bus I get a byte as information, if various things are switched off or turned on. So every bit gives me a single information. Now I have to read it out.

I tried different possibilities to read out the last two bits:

x = data && (1<<0)
x = data & (1<<0)
x = data && 0x02;
x = data && 0b00000001

Above you can see some of my trys. But none of them or others, I also tried but can't remember, work. Me sending a 0x01 or 0x02 by can, the PIC always reacts as if the second bits of the byte was set.

What would be the right way of solving this problem? I would be very thankful fpor help,

pom
Ttelmah
Guest







PostPosted: Mon Jun 06, 2005 6:44 am     Reply with quote

'&&', is 'logical' and. It returns a 'true' if both of the incoming bytes are 'true' (non zero). '&', is bitwise and, and is the function you want. You were very close with your second try, except you are just testing the low bit (you are shifting the 'mask' left by nothing...).
Now your description of what you want, is perhaps not very clear. If you want the last two bits of a value, then you need:

x=data & 0b00000011;

Much clearer to use the 'binary' format to show which bits you are testing.

If you want to know if either incoming value (data, and data1 say) has a bit in one of the other of these locations, then you would need:

x=(data & 0b00000011) || (data1 & 0b00000011);

Here the part 'data & 0b00000011' will evaluate as 'true' if either bit is set, as will the similar statement for data1, and then these are logically 'ored' to give the result fed to 'x'. This will be 'true' if either incoming byte has one of the selected bits set.

Best Wishes
pom



Joined: 25 Nov 2004
Posts: 30
Location: Germany

View user's profile Send private message

It does not work.
PostPosted: Wed Jun 08, 2005 11:02 am     Reply with quote

Hello,

I tried what Ttelmah told me to do. This is my code:

Quote:

x = (rx_data[0] & 0b00000001);
y = (rx_data[0] & 0b00000010);


But it does not work. I send messages to the PIC with a PEAKCan-module. This is, what happens:

Message hexadecimal = 0x01 brings x = 1; y = 0; so is correct.
Message hexadecimal = 0x02 brings x = 0; y = 0; y should be 1.

Another try is the following code:

Quote:

a = (rx_data[0] & 0b00000001);
b = (rx_data[0] & 0b00000011);


It also is not correct:

Message hexadecimal = 0x01 brings a = 1; b = 1; b should be 0.
Message hexadecimal = 0x02 brings a = 0; b = 0; correct, but not what I want.
Message hexadecimal = 0x03 brings a = 1; b = 1; correct, but not what I want.

One of the variables should be 1 if the last bit is 1. The other vairbale should be 1, if the second bit from behind is 1. Where is the mistake?

Thanks for helping and have a nice evening, morning, whatever.

Bye, pom
[/list]
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Jun 08, 2005 11:32 am     Reply with quote

Don't re-invent the wheel. Use the bit_test() function
Code:
if( bit_test(x,3) || !bit_test (x,1) ){//either bit 3 is 1 or bit 1 is 0
  //do something
  }

Ttelmah
Guest







PostPosted: Wed Jun 08, 2005 2:15 pm     Reply with quote

As you say, bit_test is simpler, but the functions as posted should work. What is 'y' declared as?. What is rx_data declared as?.
If you look at the manual entry for 'bit_test', you will find the equivalent function posted using a bit mask, and this is exactly the same masking operation as is being performed. The masking form is 'better' (necessary...), when the bit to be tested is a variable. If the bit is fixed, then bit_test is the more efficient implementation.

Best Wishes
Mark



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

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

PostPosted: Wed Jun 08, 2005 6:10 pm     Reply with quote

Quote:
Message hexadecimal = 0x02 brings x = 0; y = 0; y should be 1.

Wrong, it will equal 2 - 0b00000010!
pom



Joined: 25 Nov 2004
Posts: 30
Location: Germany

View user's profile Send private message

It works!!!
PostPosted: Thu Jun 09, 2005 7:12 am     Reply with quote

Thanks for help! I am really happy because of finishing my project by using the bit_test-function. I have never heard of it before. I think, I will never forget it anymore.

Have a nice day, pom
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