Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] basic question about c

Status
Not open for further replies.

syenidogan

Member level 1
Joined
Mar 6, 2014
Messages
41
Helped
2
Reputation
4
Reaction score
3
Trophy points
8
Activity points
297
if ((buffer[1] == 16)&(((unsigned int) buffer[6] + 9)!=counter))


(buffer[1]==16) gives one bit result 1 or 0
(((unsigned int) buffer[6] + 9)!=counter) gives one bit result 1 or 0

then and (&) these two values and see if it is 1 or zero ? is this correct ?


if not ,can some one explain me detailed.

buffer is unsigned char
and counter is unsigned int
 
Last edited:

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.
 

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.

buffer is unsigned char
and counter is unsigned int
 

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 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 ((buffer[1] == 16)&(((unsigned int) buffer[6] + 9)!=counter)) is it the same thing
if ((buffer[1] == 16)&&(((unsigned int) buffer[6] + 9)!=counter))

if not which one is correct? if both of them correct what is the difference?
 

2nd one is correct. My previous explanation was wrong.
 

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.
 

Maybe this is the correct one


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.

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 ?
 

I am extremely sorry. This is the correct one.


Code C - [expand]
1
if ((buffer[1] == 16)&&(((unsigned int) buffer[6] + 9)!=counter))



without && it will give error. If any one is 0 the if will not execute. if will execute if only both are 1 that is true.

true && true = true
true && false = false
false && true = false
false && false = false
 

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 ?

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.
 

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.

confused!!!! :((

- - - Updated - - -

just tell me which values should be in buffer[1] and buffer[6] if my counter is 15 to do execute return?

if ((buffer[1] == 16)&(((unsigned int) buffer[6] + 9)!=counter)) return 2 ;

buffer[1]=0x10 and buffer[6] different than 0x06 ?
 

All I'm saying is that you are using the bitwise AND operator in place of a logical AND operator. In your example both will give a correct result but the proper one is the && operator since you want to logically AND the two conditions.
 

If buffer[1] contains 16 then

Code C - [expand]
1
(buffer[1] == 16)

becomes true (1) and if counter is 15 then counter - 9 = 15 - 9 = 6. So, buffer[6] should contain 6. If buffer[1] = 16 and buffer[6] = 6 then both conditions become true (1) and hence if condition will execute.
 

X!=5. Here if x is 5 this gives false otherwise it gives true. ?
 

Yes.

If buffer[1] contains 16 then (buffer[1] == 16)
becomes true (1) and if counter is 15 then counter - 9 = 15 - 9 = 6. So, buffer[6] should not contain 6. If buffer[1] = 16 and buffer[6] = 6 then both conditions become true (1) and hence if condition will execute.
 

If counter = 15 then buffer[6] + 9 should not be equal to 15.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top