What is the output of a bitwise AND operation if one of the inputs operands is 0?

Bits and Bytes

We know that memory is made up of bits and bytes, and we should know what they are by now.
  • A bit is the smallest unit of storage in a computer.  It stores a 0 or a 1.
  • A byte consists of 8 bits, and is special because it is usually the smallest unit of directly addressible storage.  This means - it is the smallest item that we can create a variable out of.  Addresses in memory are usually applied to bytes.
The smallest built-in data type is the char, which on most systems today is 1 byte.
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

 
OperatorNameArityDescription
& bitwise AND binary Similar to the && operator, but on a bit-by-bit basis.  Bits in the result set to 1 if corresponding operand bits are both 1, and set to 0 otherwise
| bitwise OR (inclusive) binary Similar to the || operator, but on a bit-by-bit basis.  Bits in the result set to 1 if at least one of the corresponding bits in the two operands is 1.  0 otherwise.
^ bitwise exclusive OR binary Bits in the result set to 1 if exactly one of the corresponding bits in the two operands is 1.  Result bit set to 0 if both corresponding bits in operands are the same.
<< left shift binary Shifts the bits of the first operand to the left, by the number of bits specified in the second operand.  Right fill with 0 bits.
>> right shift binary Shifts the bits of the first operand to the right, by the number of bits specified in the second operand.  Left fill depends on the machine.  Usually based on the sign (fill with 0's for positive numbers, 1's for negatives).
~ complement unary Flips the bits in the operand.  Similar to negation.  (All 1's become 0's, and all 0's become 1's).

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

Examples

Suppose 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 00110110
Suppose 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 2082

Here is the bitwise OR operation (x | y):
     x:  00011010 11101011
     y:  00101100 00110110
--------------------------
result:  00111110 11111111    // this is the value 16127

Here is the bitwise exclusive OR operation (x ^ y):
     x:  00011010 11101011
     y:  00101100 00110110
--------------------------
result:  00110110 11011101    // this is the value 14045

Here is a bitwise left shift, performed on x (x << 2):
      x:  00011010 11101011
---------------------------
shifted:  01101011 10101100    // this is the value 27564

Here is a bitwise right shift, performed on y (y >> 4):
      y:  00101100 00110110
---------------------------
shifted:  00000010 11000011    // this is the value 707

And here is the complement of x (~x)
      x:  00011010 11101011
---------------------------
     ~x:  11100101 00010100    // this is the value -6892

Code examples

  • Here is a sample program that performs these operations (from the examples above) on these two numbers.
  • Here are a few bitwise operator examples from Deitel (a previous textbook)
    • Fig22_06.cpp
    • Fig22_08.cpp
    • Fig22_11.cpp
  • Here's the start of a class called BitFlags, set up to store a set of on/off flags with simple internal storage.
    • On a system with 4-byte integers, a BitFlags object can store 32 flags (using one variable for all the flags)
    • A couple of functions are implemented. The others are left as an exercise for the reader

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.