I\'ve seen many instances where |
or &
are used, but I haven\'t understood what they are used for. I know what &&
and ||<
You can read about them and their differences in Swift docs.
Logical Operators 1
Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:
Logical NOT (!a) Logical AND (a && b) Logical OR (a || b)
Bitwise Operators 2
Bitwise operators enable you to manipulate the individual raw data bits within a data structure. They are often used in low-level programming, such as graphics programming and device driver creation. Bitwise operators can also be useful when you work with raw data from external sources, such as encoding and decoding data for communication over a custom protocol.
Swift supports all of the bitwise operators found in C, as described below.
&
and |
are bitwise operators that is they compare the bit representation(binary value).
`&` -bitwise AND operator
`|` -bitwise OR operator
&&
and ||
are used to compare the actual values in case of primitives like int,double,etc.
and the address in case of objects.They are called logical operators.
1. Lets go to an example to make it simpler (bitwise operator):
int p=9; int q=10;
//binary representation of 9 and 10 are 1001 and 1010 respectively.
Now, p & q
returns a one in each bit position for which the
corresponding bits of both operands are ones.
p|q
returns a one in each bit position for which the corresponding bits of either or both operands are ones.
2.An example for logical operators.
int p=9; int q=10;
p>0 && q>0
returns true since p
and q
are both greater than 0
(since the conditions on either side of the operator is true).
p>0 || q<0
returns true since at least one condition is true(here,p is greater than 0).
I hope that this has helped you to understand the difference between bitwise
and logical
operators.
See the Wikipedia page on Bitwise Operation and the Swift documentation for bitwise operators.
These are bitwise operators. &
is bitwise AND and |
is bitwise OR.
See these examples:
0011 (decimal 3)
AND 0010 (decimal 2)
= 0010 (decimal 2)
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
Source: Wikipedia
The uses of bitwise operators have been discussed before on StackOverflow:
A use of bitwise XOR (not in your question, but a cool logic gate anyway) that caught my eye (by @Vilx- here) (I don't know how it works, but the answer was accepted and up-voted 34 times)
EDIT: If you want to know how it works, there's a nice simple proof over on XOR swap algorithm - Wikipedia
Swapping two integer variables without an intermediary variable:
A = A^B // A is now XOR of A and B
B = A^B // B is now the original A
A = A^B // A is now the original B
If these don't help, the Wikipedia page I already linked to twice in this post has an Applications section, but they don't really apply to higher-level languages (unless for some reason you want to optimize your arithmetic to use only bitwise operations).