== is a test for equality. = is an assignment.
Any good C book should cover this (fairly early on in the book I would imagine).
For example:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
Just watch out for the heinous:
if (i = 4) { }
which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:
||
&&
b4
No magic there.
Answer from paxdiablo on Stack OverflowVideos
== is a test for equality. = is an assignment.
Any good C book should cover this (fairly early on in the book I would imagine).
For example:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
Just watch out for the heinous:
if (i = 4) { }
which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:
||
&&
b4
No magic there.
a == b is a test if a and b are equal.
a = b is called an assignment, which means to set the variable a to having the same value as b.
(You type | with Shift-\ in the US keyboard layout.)
The first operation is called bitwise OR | and it works as follows imagining the numbers A=200 and B=184, which in binary will be, 11001000 and 10111000, respectively. The operator "|" will compare bit a bit of each of those numbers, and returns 1 when either of the bits is 1, 0 otherwise. So in your case:
11001000
| 10111000
--------
= 11111000
the result will be 248 (11111000 in binary). Therefore DDRB |= 0b00000001; is DDRB = DDRB | 0b00000001.
The second operation (e.g., DDRB = 0b00000001) is just an assignment of a value to a variable.
Do the two lines have same effect of writing one to data register B ?
No.
DDRB = 0b00000001;
writes one to bit-0 and zero to bits 1 to 7, while:
DDRB |= 0b00000001;
is a read-modify-write operation equivalent to:
DDRB = DDRB | 0b00000001;
so it writes a one to bit-0 as before and leaves all other bits unchanged.
So for example if DDRB's current value were 0b11110000 after:
DDRB = 0b00000001;
it will be 0b00000001 while after:
DDRB |= 0b00000001;
it will be 0b11110001.
So one sets DDRB to 1, while the other sets DDRB:Bit-0 to 1.
= is an assignment operator in C. According to C99 6.5.16:
An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
It means that expression a = 5 will return 5 and therefore instructions inside if block will be executed. In contrary, if you replaced it with a = 0 then 0 would be returned by assignment expression and instructions inside if would not be executed.
A single = means an assignment operator, that means you are changing the value of a and using this value in the if statement.
if(a = 5) {
// some code
}
is the same of:
a = 5;
if(a) {
// some code
}
The == means a operation of logical equivalence, witch will return 1 if the two values are the same or 0 if they aren't the same.
I'm just starting and really confused. Thanks!