What is bitwise?Bitwise is a level of operation that involves working with individual bits which are the smallest units of data in a computing system. Each bit has single binary value of 0 or 1. Most programming languages manipulate groups of 8, 16 or 32 bits. These bit multiples are known as bytes. Show
The arithmetic logic unit (ALU) is a part of a computer's CPU. Inside the ALU, mathematical operations like addition, subtraction, multiplication and division are all done at bit level. For those operations, bitwise operators are used. Bitwise operationsA bitwise operation operates on two-bit patterns of equal lengths by positionally matching their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both the first AND second bits are 1. If only one bit is a 1, the result is 0. AND can also be used to test individual bits in a bit string to see if they are 0 or 1. A logical OR (|) operation functions differently from the AND operations. For each bit pair, the result is 1 if the first OR second bit is 1. If neither bit is 1, the result is 0. A logical XOR (~) of each bit pair results in a 1 if the two bits are different, and 0 if they are the same (both zeros or both ones). Logical NOT is represented as ^. Left shift (<<), right shift (>>) and zero-fill right shift (>>>) bitwise operators are also known as bit shift operators. Arithmetic logic unit, part of a computer CPU, is where bitwise operators used to perform mathematical operations.Bitwise operatorsBitwise operators are characters that represent actions (bitwise operations) to be performed on single bits. They operate at the binary level and perform operations on bit patterns that involve the manipulation of individual bits. Thus, unlike common logical operators like + or - which work with bytes or groups of bytes, bitwise operators can check each individual bit within a byte. The most common bitwise operators used in C/C++ are given in the table below.
Multiple bitwise operators are used in bit manipulation. These operations happen very fast and optimize system performance and time complexity. It's important to keep in mind that the left shift and right shift operators should not be used for negative numbers. Doing this can result in undefined behaviors in the programming language. Also, bitwise operators should not be used in place of logical operators because they work differently. Logical operators consider non-zero operands as 1 and their result is either 0 or 1. In contrast, bitwise operators return an integer value. The table below defines the JavaScript bitwise operators.
Applications of bitwise operations and operatorsThere are many applications of bitwise operations and operators. For one, they are used in data compression where data is converted from one representation to another to reduce the amount of storage space required. Bitwise operations are also used in encryption algorithms to encrypt data and protect it from unauthorized use, manipulation or exfiltration. The following are some other common applications:
Bitwise ANDThe bitwise AND operator produces an output of 1 if the corresponding bits of both the operands are 1. If not, the output is 0. Example 1: Bitwise AND operation of two one-bit operands.
Example 2: Bitwise AND operation of two integers: 28 and 17; the & operator compares each binary digit of these integers.
Thus: 28 & 17 (bitwise AND) = 00010000 (binary) = 16 (decimal). Bitwise ORThe bitwise OR operator produces an output of 1 if either one of the corresponding bits is 1. Otherwise, the output is zero. Example 1: The bitwise OR operation of two one-bit operands.
Example 2: Let's consider the previous example of two integers: 28 and 17.
Thus: 28 | 17 (bitwise OR) = 00011101 (binary) = 29 (decimal). Bitwise exclusive OR (XOR)The bitwise exclusive OR (XOR) operator returns 1 if the bits of both operands are opposite. Otherwise, it returns 0. Example 1: The bitwise XOR operation of two one-bit operands.
Example 2: Let's see how bitwise XOR works for our two integers 28 and 17.
Thus: 28 ^ 17 (bitwise XOR) = 00001101 (binary) = 13 (decimal). Bitwise NOTThe bitwise NOT operator reverses the bits. Unlike other bitwise operators, it accepts only one operand. Example: Let's consider the bitwise NOT operation of the integer 28.
Thus: ~28 (bitwise NOT) = 11100011 (binary) = 227 (decimal). Bitwise left shiftThe bitwise left shift operator shifts the bits left by the bits specified by the right operand. The positions vacated by the left shift operator are filled with 0. Example: Let's perform the bitwise left shift operation on the integer 6. Each bit will be shifted left by 1. 6 = 0110 6<<1 = 1100 (binary) = 12 (decimal) Bitwise right shiftLike the left shift operator, the bitwise right shift operator shifts the bits right by the bits specified by the right operand. The positions vacated by the right shift operator are filled with 0. Example: Let's perform the right shift by two bits operations on the integer 8. Each bit will be shifted right by 2. 8 = 1000 8>>2 = 0010 (binary) = 2 (decimal)
See also: bit stuffing, logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR), How many bytes for..., classical computing, Advanced Business Application Programming What is the output of an Exclusive OR () operation if one of the operands is false?4) What is the output of an Exclusive OR(^) operation, if one of the operands is false? Explanation: Exclusive OR (^) gives an output of true if both the operands are different. If both are the same (true / false), the output is false.
What is the output of Exclusive OR operator if one of the operands is true?|| (Logical OR) operator
If one of the operands or expressions is true, it will return 1.
What is the output of logical or operator 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.
What is the output of a logical OR operation if one of the expression is true?The logical OR ( || ) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.
|