Since C was designed to take the place of assembly language for most programming tasks, it needed to be able to support assembler – like operations. Bitwise operations refer to testing, setting or shifting the actual bits in a byte or word (byte and word corresponding to the types char and int and their variants). C’s bitwise operators are listed in below table
There are three logical bitwise operators; bitwise AND (&), bitwise EXCLUSIVE OR (^), and bitwise or (|). Each of these operators requires two integer – type operands. The operations are carried out independently on each pair of corresponding bits within the operands. Thus, the least significant bits (i.e. the rightmost bits) within the two operands will be compared, then the next least significant bits, and so on, until all of the bits have been compared. The results of these comparisons are:
A bitwise AND expression will return a 1 if both bits have a value of 1 (i.e., if both bits are true). Otherwise, it will return a value of 0.
A bitwise EXCLUSIVE OR expression will return a 1 if one of the bits has a value of 1 and the other has a value of 0 (one bit is true, the other false). Otherwise, it will return a value of 0.
A bitwise OR expression will return a 1 if one or more of the bits have a value of 1 (one or both bits are true ). Otherwise, it will return a value of 0.
These results are summarized in table 3.5 . In this table, a1 and a2 represent the corresponding bits are within the fast and second operands, respectively.
Bitwise operators
Each of the logical bitwise operators has its own precedence. This bitwise AND (&) operator has the highest precedence, followed by bitwise EXCLUSIVE OR (^), then bitwise or (|).
Bitwise ANDing is frequently used for quotmasking operation, i.e., the operator may be used to set specific bits to zero. Any bit that is 0 in operand causes the corresponding bit to be set to 0.
Similarly, the bitwise OR may be used to set a bit. Any which is set to 1 in either operand cause the outcome to be set to 1.
Two bitwise shift operators are left shift (<<) and right shift (>>). Each operator requires two operands. The first is an integer- type operand that represents the bit pattern to be shifted. The second is an unsigned integer that indicates the number of displacements i.e., the number of bit position the first operand is shifted.
The One's Complement Operator reverses the state of each bit in the specified variable. Note that two complements in a row return to its original value.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc