How to perform the bitwise NOR operation in Swift

Overview

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:

  • The OR (|) and the NOT (~ ) operators
  • The AND (&) and the NOT (~ ) operators

Method 1: Using OR (|) and NOT (~)

In this method, we first perform the bitwise OR (|) operation on two numbers, and then invert the result using the NOT (~) operator.

Syntax

The syntax for the bitwise NOR operator is as follows:

~ (LHS | RHS)

Parameters

  • LHS: This is the integer on the left-hand side.
  • RHS: This is the integer on the right-hand side.

Return value

The possible return values of the bitwise NOR operation on different combinations of bits are summarized below:

The possible return values of NOR

LHS

RHS

LHS | RHS

~(LHS | RHS)

1

1

1

0

1

0

1

0

0

1

1

0

0

0

0

1

Code

The code for performing the bitwise NOR operation on two numbers using the OR and NOT operators is as follows:

// Declare two integer variables
var firstNumber = 10 // Binary: 00001010
var secondNumber = 20 // Binary: 00010100
// Perform the bitwise NOR operation
var resultNumber = ~(firstNumber | secondNumber)
print(resultNumber) //-31. Binary: 11100001

Explanation

  • Lines 2–3: We declare two new variables, firstNumber and secondNumber. We assign them the integer values 10 (00001010 in binary) and 20 (00010100 in binary), respectively.
  • Line 6: We perform the bitwise NOR operation. We transverse the firstNumber and secondNumber variables bitwise. The resulting bit sets to 11 if one of the bits is 11. Otherwise, they are set to 00. The resulting number is inverted.

The complete calculation is shown below:

Using the bitwise NOR operation on two integers, 10 and 20

To convert a binary into a negative decimal, the left-most bit represents the sign (negative if 11, and positive if 00). In the example above, the left-most bit is 11, so the answer is a negative value.

The calculation is as follows: resultNumber=(127)+(126)+(125)+(124)+(023)+(122)+(021)+resultNumber=(-1*2^7)+(1*2^6)+(1*2^5)+(1*2^4)+(0*2^3)+(1*2^2)+(0*2^1)+

Method 2: Using AND (&) 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.

Syntax

The syntax for the bitwise NOR operator is as follows:

~num1 & ~num2

Return value

The possible return values of the bitwise NOR operation on different combinations of bits are summarized below:

The possible return values of NOR

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

Code

The code for performing the bitwise NOR operation on two numbers using AND and NOT operators is as follows:

// Declare two integer variables
var firstNumber = 10 // Binary: 1010
var secondNumber = 20 // Binary: 10100
// Perform the bitwise NOR operation
var resultNumber = ~firstNumber & ~secondNumber
print(resultNumber) //-31. Binary: 11100001

Explanation

  • Lines 2–3: We declare two new variables, firstNumber and secondNumber. We assign them the integer values 10 (00001010 in binary) and 20 (00010100 in binary), respectively.
  • Line 6: We perform the bitwise NOR operation. The traverse the firstNumber and secondNumber variables bitwise, and the resulting bit is set to 11 if it is 00. Otherwise, they are set to 00. Next, we perform the bitwise AND operation on the inverted values of firstNumber and secondNumber.

The complete calculation is as follows:

Using the bitwise NOR operation on two integers, 10 and 20

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved