Zu den Bitoperatoren gehören:
| & | AND (UND) |
| &= | AND (UND) |
| | | OR (ODER) |
| |= | OR (ODER) |
| ^ | XOR(Exklusiv-ODER) |
| ^= | XOR (Exklusiv-ODER |
| ~ | Einerkomplement |
| << | Shift Left (Linksschieben |
| << | Shift Left (Linksschieben) |
| >>= | Shift Right (Rechtsschieben) |
| >>= | Shift Right (Rechtsschieben) |
Diese Operationen erfordern zwei Operanden und führen Bitvergleiche durch.
AND & kopiert ein Bit zum Ergebnis, wenn es in beiden Operanden gleich 1 ist.
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
unsigned int c = 0;
c = a & b; /* 12 = 0000 1100 */
}
|
OR | kopiert ein Bit zum Ergebnis, wenn es in mindestens einem Operanden gleich 1 ist..
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
unsigned int c = 0;
c = a | b; /* 61 = 0011 1101 */
}
|
XOR ^ kopiert ein Bit zum Ergebnis, wenn es in einem (nicht beiden) Operand gleich 1 ist..
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
unsigned int c = 0;
c = a ^ b; /* 49 = 0011 0001 */
}
|
Try it out with one of these simulators.
This operator is unary (requires one operand) and has the efect of 'flipping' bits.
main()
{
unsigned int Value=4; /* 4 = 0000 0100 */
Value = ~ Value; /* 251 = 1111 1011 */
}
|
Try it out
The following
operators can be used for shifting bits left or right.| << | >> | <<= | >>= |
The left operands value is moved left or right by the number of bits specified by the right operand. For example:
main()
{
unsigned int Value=4; /* 4 = 0000 0100 */
unsigned int Shift=2;
Value = Value << Shift; /* 16="0001" 0000 */ Value <<="Shift;" /* 64="0100" 0000 */ printf("%d\n", Value); /* Prints 64 */ }
|
Usually, the resulting 'empty' bit is assigned ZERO. Please use unsigned variables with these operators to avoid unpredictable results.
Try it out with one of these silulators
AND
OR
Bit shifting.
Bit shifting problem.
All the other Ausdrucks and operators.
Operator precedence.
Assignment Operators.