The bitwise NOR operation in Swift negates the logical OR operation on two 8-bit binary numbers. It does not have an assigned operator, so we perform it by the following two combinations:
|
) and NOT (~
) In this method, we first perform the bitwise OR (|
) operation on two numbers, and then invert the result using the NOT (~
) operator.
The syntax for the bitwise NOR operator is as follows:
~ (LHS | RHS)
LHS
: This is the integer on the left-hand side.RHS
: This is the integer on the right-hand side.The possible return values of the bitwise NOR operation on different combinations of bits are summarized below:
LHS | RHS | LHS | RHS | ~(LHS | RHS) |
1 | 1 | 1 | 0 |
1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 |
0 | 0 | 0 | 1 |
The code for performing the bitwise NOR operation on two numbers using the OR and NOT operators is as follows:
// Declare two integer variablesvar firstNumber = 10 // Binary: 00001010var secondNumber = 20 // Binary: 00010100// Perform the bitwise NOR operationvar resultNumber = ~(firstNumber | secondNumber)print(resultNumber) //-31. Binary: 11100001
firstNumber
and secondNumber
. We assign them the integer values 10
(00001010 in binary) and 20
(00010100 in binary), respectively.firstNumber
and secondNumber
variables bitwise. The resulting bit sets to The complete calculation is shown below:
To convert a binary into a negative decimal, the left-most bit represents the sign (negative if
The calculation is as follows:
&
) and NOT (~
) In this method, we first perform the bitwise NOT (~
) operation on each of the two numbers and then apply the bitwise AND (&
) operation on the resulting two numbers.
The syntax for the bitwise NOR operator is as follows:
~num1 & ~num2
The possible return values of the bitwise NOR operation on different combinations of bits are summarized below:
LHS | ~LHS | RHS | ~RHS | ~LHS & ~RHS |
1 | 0 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
The code for performing the bitwise NOR operation on two numbers using AND and NOT operators is as follows:
// Declare two integer variablesvar firstNumber = 10 // Binary: 1010var secondNumber = 20 // Binary: 10100// Perform the bitwise NOR operationvar resultNumber = ~firstNumber & ~secondNumberprint(resultNumber) //-31. Binary: 11100001
firstNumber
and secondNumber
. We assign them the integer values 10
(00001010 in binary) and 20
(00010100 in binary), respectively.firstNumber
and secondNumber
variables bitwise, and the resulting bit is set to firstNumber
and secondNumber
. The complete calculation is as follows:
Free Resources