syenidogan
Member level 1
- Joined
- Mar 6, 2014
- Messages
- 41
- Helped
- 2
- Reputation
- 4
- Reaction score
- 3
- Trophy points
- 8
- Activity points
- 297
What type of variables are counter, and buffer[] ?
Here it is checked if buffer[1] equals 16 and buffer[6] casted to unsigned int + 9 not equal to value in counter variable.
If buffer[1] equals to 16 then it gives 1 (true) and if not equal it gives 0 (false)
Then buffer[6] is casted to unsigned int. If buffer[6] which is unsigned char (unsigned char can hold 256 values, 0-255) then it is casted to unsigned int which will also have the same value. I don't know why casting is done here. It doesn't make any difference. Then 9 is added to this value and it is compared whether this value is not equal to value in counter. If it is not equal you get 1 (true) and you get 0 (false) if it is equal to counter. Then if you get both trues then the if condition is executed.
I think && is typed as & in your code.
if( (PINB & (1<<1)) & (PINC & (1<<2)) )
if( (0x02)) & (0x04)) )
if( (0x02)) && (0x04)) )
if( (1)) && (1)) )
Code C - [expand] 1 if( ( (buffer[1] == 16) && ((unsigned int) buffer[6] + 9)) ) != counter)
I think he is asking if its a problem to use a bitwise AND (&) in place of a logical AND (&&).
It may work in some cases but you are better off using && which is the proper one for logic operations.
Suppose that you want to check if two bits are high
Code:if( (PINB & (1<<1)) & (PINC & (1<<2)) )
Assume that this equals
Code:if( (0x02)) & (0x04)) )
this would give a false result because ((0x2) & (0x04)) results to 0
Now if this was written with &&
Code:if( (0x02)) && (0x04)) )
then it would be the equivalent of
Code:if( (1)) && (1)) )
and the result would be true
&& between non 0 values results to 1.
Code C - [expand] 1 if ((buffer[1] == 16)&&(((unsigned int) buffer[6] + 9)!=counter))
this means that (buffer[1]==16) if it gives zero and (((unsigned int) buffer[6] + 9)!=counter) if this also gives zero
then if do & i will get zero but if i do && i will get 1 ?
0x00 & 0x00 results to 0
0x01 & 0x00 results to 0
0x01 & 0x01 results to 1
0x01 & 0x02 results to 0
0x02 & 0x02 results to 2
0x00 & 0x00 results to 0
0x01 & 0x00 results to 1
0x01 & 0x01 results to 1
0x01 & 0x02 results to 1
0x02 & 0x02 results to 1
No
Code:0x00 & 0x00 results to 0 0x01 & 0x00 results to 0 0x01 & 0x01 results to 1 0x01 & 0x02 results to 0 0x02 & 0x02 results to 2 0x00 & 0x00 results to 0 0x01 & 0x00 results to 1 0x01 & 0x01 results to 1 0x01 & 0x02 results to 1 0x02 & 0x02 results to 1
&& between 0 values results to 0.
&& between non 0 values results to 1.
Code C - [expand] 1 (buffer[1] == 16)
If counter = 15 then buffer[6] + 9 should not be equal to 15.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?