Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

"|=" operator in C Language: Solved

Status
Not open for further replies.

eblc1388

Active Member
Can't find anything using "|=" as search phase in Google.

Found it at last. It is bitwise inclusive OR. So A |= B means A = A .OR. B
 
eblc1388 said:
Joel Rainville said:
What about ^= and &= ?

;):lol:

Both "|=" and "^=" resulted in nothing in Google. "&=" works the same as "&".

Yep :

Code:
(A = A | B) == (A |= B) // inclusive OR
(A = A ^ B) == (A ^= B) // eXclusive OR
(A = A & B) == (A &= B) // AND

The unary NOT operator is !, and of course the != operator isn't a bitwise operator, but a comparison operator meaning "different than"...

I guess Google has its limits :D
 
Joel Rainville said:
The unary NOT operator is !, and of course the != operator isn't a bitwise operator, but a comparison operator meaning "different than"...

So "!=" is the XOR instruction in C then?
 
eblc1388 said:
Joel Rainville said:
The unary NOT operator is !, and of course the != operator isn't a bitwise operator, but a comparison operator meaning "different than"...

So "!=" is the XOR instruction in C then?

It is already mentioned that ! is a unary operator. This means it will only invert 0 to 1 and 1 to 0.
But that is true only if the data is in bytes
This means if you write 0XFF != a, then a will contain 0 and if you again invert it by saying !a then the answer will be 1
in binary
!(0000 0000) = (0000 0001)
whereas if you use ~ then it will simply invert each bit as it is a bitwise operator.

NOT operators are unary ie operate on single bit or byte
 
sarang1_in said:
It is already mentioned that ! is a unary operator. This means it will only invert 0 to 1 and 1 to 0.

But "!=" means "not equal". If A & B are Boolean, then A != B means !(A==B), right?

So is it the same as A XOR B, assuming there is a logical XOR instruction in C?
 
sarang1_in said:
eblc1388 said:
Joel Rainville said:
The unary NOT operator is !, and of course the != operator isn't a bitwise operator, but a comparison operator meaning "different than"...

So "!=" is the XOR instruction in C then?

It is already mentioned that ! is a unary operator. This means it will only invert 0 to 1 and 1 to 0.
But that is true only if the data is in bytes
This means if you write 0XFF != a, then a will contain 0 and if you again invert it by saying !a then the answer will be 1
in binary
!(0000 0000) = (0000 0001)
whereas if you use ~ then it will simply invert each bit as it is a bitwise operator.

NOT operators are unary ie operate on single bit or byte

This is misleading.

!= is a logical (or comparison) operator that evaluates to a boolean value of 0 for false or 1 for true. So the following is true, but not for the reason you're thinking :

This means if you write 0XFF != a, then a will contain 0 and if you again invert it by saying !a then the answer will be 1

The same goes for !, which is also a logical operator.

The bitwise NOT operator in C is ~ [edit: previously wrong statement edited out]


eblc, the XOR operator in C is ^. != is a comparison operator, just like ==.

To sum it up :

Comparison operators that evaluate to 0 or 1 : ==, !=, &&, ||
Binary operators (taking 2 operands) : +, -, ^, |, & and their "lazy programmer" versions : +=, -=, ^=, |=, &=
Unary operators : !, ++, --
 
Joel Rainville said:
eblc, the XOR operator in C is ^.

Yes, but "^" is a bitwise operator.

If A & B are Boolean, how to express A XOR B in C Language? Still A^B ???
 
eblc1388 said:
But "!=" means "not equal". If A & B are Boolean, then A != B means !(A==B), right?

Yes, you got it.

So is it the same as A XOR B, assuming there is a logical XOR instruction in C?

A logical XOR doesn't exist in C because it doesn't evaluate strictly to boolean values on non-boolean operands... But you do have a bitwise XOR ;)
 
eblc1388 said:
Joel Rainville said:
eblc, the XOR operator in C is ^.

Yes, but "^" is a bitwise operator.

If A & B are Boolean, what is A XOR B in C Language? Still A^B ???

Yes... I don't understand why you insist on A & B being boolean though, since it doesn't change anything?

Say A = 0001, B = 0000. (A ^ B) == 0001
Say A = 1010, B = 0101. (A ^ B) == 1111

As you can see, what the ^ operator can't do is give you a straight boolean value from non-boolean operands. That's why there is no ^^ logical (or comparison) operator.
 
Joel Rainville said:
Yes... I don't understand why you insist on A & B being boolean though, since it doesn't change anything?

Because they are "states" of something like switches, pin logic level...i.e. condition.

I have a perfectly good reason to test that "Either my switch1 or switch2 is closed but not both" and this needs a XOR test.

So we come back to the original question, can A!=B gives me the required result?
 
eblc1388 said:
Joel Rainville said:
Yes... I don't understand why you insist on A & B being boolean though, since it doesn't change anything?

Because they are "states" of something like switches, pin logic level...i.e. condition.

I have a perfectly good reason to test that "Either my switch1 or switch2 is closed but not both" and this needs a XOR test.

So we come back to the original question, can A!=B gives me the required result?

Yes, but it's not a XOR, it's just plain english ;) It litterally means "A is not equal to B." It just happens to give the same result as a XOR when A and B are boolean.
 
To elaborate a little, != on the PIC would probably compile into a corresponding XOR operation. Other RISC platforms might do the same. But not all platforms would do it like that, especially CISC platforms.
 
It is all how you use the things.

!= can be (In normal cases always be) used in conditional loops like
Code:
 if(i != j)
Code:
while(n != p)

!= = Not equal to

Now case 2 is as follows

Code:
i += 1

will result in
Code:
 i = i + 1

same with - , && ! and ||,~,!,&,^,|,*,/

Now here one must be very careful about bitwise operators and bytewise operators

Bitwise and &
Bytewise and &&
Bitwise or |
Bytewise or ||
Bitwise not ~
Bytewise not !

While writing code in embedded C, few instructions are supported though they are not ANSI standard just to match some assembly instructions.
What if I want to rotate accumulator through carry?

So there should be no confusion in that.
!= is not XOR as
Code:
a != b can't be 
!(a == b) but is 
a = !b
 
While we can use "&&" to test for both conditions to be true and "||" for either one to be true, I now wonder whether it is legal in C to write:

/* condition1: true/false
condition2: true/false */

If (condition1 != condition2)
{....}

Possible workaround:

If ( (condition1 && !condition2) || (!condition1 && condition2) )
{....}

But it seems too long-winded.
 
sarang1_in said:
Bitwise and &
Bytewise and &&
Bitwise or |
Bytewise or ||
Bitwise not ~
Bytewise not !

There is no such thing as a "bytewise" operator in C. I don't know where you got this idea. There are bitwise and logical operators that are either unary (taking 1 operand) or binary (taking 2 operands). What you describe as "bytewise" operators are really logical operators that make an expression like (A && B) evaluate to a boolean value of 0 or 1, no matter the size of A and B.
 
eblc1388 said:
While we can use "&&" to test for both conditions to be true and "||" for either one to be true, I now wonder whether it is legal in C to write:

/* condition1: true/false
condition2: true/false */

If (condition1 != condition2)
{....}

Possible workaround:

If ( (condition1 && !condition2) || (!condition1 && condition2) )
{....}

But it seems too long-winded.

It is perfectly legal... although you would get fired from any software development firm for driving your coworkers to insanity! :p Your "workaround" is very hard to read. ;)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top