Show
Bits and BytesWe know that memory is made up of bits and bytes, and we should know what they are by now.
So... what if we want to access individual bits? Is this possible? Yes -- but not directly. We must use the bitwise operators to act at the bit level. Caution: Bitwise operations may be machine-dependent, to an extent, because they rely on the way that the bits are arranged in memory. Some machines may not store all built-in data types in the same
formats. Most machines, however, tend to use a pretty standard representation for integer types, called "2's Complement" format. The Bitwise Operators
In addition to these, there are also shortcut assignment operators, like with the arithmetic operators. x &= y means x = x & y x |= y means x = x | y x ^= y means x = x ^ y x <<= y means x = x << y x >>= y means x = x >> y ExamplesSuppose we have the following code:short x = 6891; short y = 11318;And let's assume that we are using a machine in which a short is 2 bytes (which is 16 bits). The binary representations of x and y, then, are: x: 00011010 11101011 y: 00101100 00110110Suppose we did the operation (x & y). Then we perform the AND operation on the individual bits: x: 00011010 11101011 y: 00101100 00110110 -------------------------- result: 00001000 00100010 // this is the value 2082Here is the bitwise OR operation (x | y): x: 00011010 11101011 y: 00101100 00110110 -------------------------- result: 00111110 11111111 // this is the value 16127Here is the bitwise exclusive OR operation (x ^ y): x: 00011010 11101011 y: 00101100 00110110 -------------------------- result: 00110110 11011101 // this is the value 14045Here is a bitwise left shift, performed on x (x << 2): x: 00011010 11101011 --------------------------- shifted: 01101011 10101100 // this is the value 27564Here is a bitwise right shift, performed on y (y >> 4): y: 00101100 00110110 --------------------------- shifted: 00000010 11000011 // this is the value 707And here is the complement of x (~x) x: 00011010 11101011 --------------------------- ~x: 11100101 00010100 // this is the value -6892 Code examples
What is the output of a bitwise AND operation if one of the inputs is zero?The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. In C Programming, the bitwise AND operator is denoted by & .
What is the Bitwise operator used set a particular bit to zero 0?The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
What is the output of a bitwise exclusive OR operation if both of the inputs operands are 0s or 1s?19) What is the output of a Bitwise Exclusive OR (^) operation if both of the inputs/operands are 0s or 1s? Explanation: 0 ^ 0 = 0; 1 ^ 1 = 1; 20) What is the output of a Bitwise Exclusive OR (^) operation if both the inputs/operands are different?
What is the output of a logical or 1 operation if one of the inputs operands is false?Logical OR operator give true as output if at least one of the operands is true. 13) What is the output of Logical AND (&) operation if one of the inputs/operands is false? Explanation: false & (anything) is false.
|