|
|
View previous topic :: View next topic |
Author |
Message |
pom
Joined: 25 Nov 2004 Posts: 30 Location: Germany
|
Compare two bytes with AND |
Posted: Mon Jun 06, 2005 6:30 am |
|
|
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
|
|
Posted: Mon Jun 06, 2005 6:44 am |
|
|
'&&', 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
|
It does not work. |
Posted: Wed Jun 08, 2005 11:02 am |
|
|
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
|
|
Posted: Wed Jun 08, 2005 11:32 am |
|
|
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
|
|
Posted: Wed Jun 08, 2005 2:15 pm |
|
|
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
|
|
Posted: Wed Jun 08, 2005 6:10 pm |
|
|
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
|
It works!!! |
Posted: Thu Jun 09, 2005 7:12 am |
|
|
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 |
|
|
|
|
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
|