~ (Boolean Not) operator

Available with Spatial Analyst license.

Available with Image Analyst license.

Summary

Performs a Boolean-complement (Not) operation on the cell values of the input raster.

Illustration

Boolean Not illustration
OutRas = ~ Raster("InRas1")

Discussion

The ~ operator will perform a Boolean Not operation when the input (operand) is a raster. If the input (operand) is a number, then the ~ operator will perform a Bitwise Not operation. For more information on how to work with operators, refer to Working with operators.

When multiple operators are used in an expression, they are not necessarily executed in left-to-right order. The operator with the highest precedence value will be executed first. For more information, see the operator precedence table in Work with operators in Map Algebra. You can use parentheses to control the execution order.

Boolean (~, &, ^, |) operators have a higher precedence level than Relational (<, <=, >, >=, ==, !=) operators. Therefore, when Boolean operators are used in the same expression as Relational operators, the Boolean operators will be executed first. To change the order of execution, use parentheses.

When multiple Relational and/or Boolean operators are used consecutively in a single expression, in some cases, it may fail to execute. To avoid this potential problem, use appropriate parentheses in the expression so that the execution order of the operators is explicitly defined. For more information, see complex statement rules section of Build complex statements.

Only a single input is necessary for the Boolean evaluation to take place.

If the input values are floating point, they are converted to integer values by truncation before the Boolean operation is performed. The output values are always integer.

If the input is a multiband raster, the output will be a multiband raster. The operator will perform the operation on each band in the input.

If the input is a multidimensional raster, all slices from all variables will be processed, and the output will be a multidimensional raster.

Parameters

OperandExplanationData Type
in_raster_or_constant

The input to use in the Boolean Not operation.

Raster Layer | Constant

Code sample

~ (Boolean Not) example 1 (Python window)

This sample performs a Boolean Not (complement) operation on an input raster.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outBooleanNot = ~ Raster("degs")
outBooleanNot.save("C:/sapyexamples/output/outboolnot.tif")
~ (Boolean Not) example 2 (stand-alone script)

This sample performs a Boolean Not (complement) operation on an input raster.

# Name: Op_BooleanNot_Ex_02.py
# Description: Performs a Boolean complement (NOT) operation on the
#              cell values of an input raster
# Requirements: Spatial Analyst Extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/sapyexamples/data"

# Set local variables
inRaster = Raster("degs")

# Execute BooleanNot
outBooleanNot = ~ inRaster

# Save the output 
outBooleanNot.save("C:/sapyexamples/output/outboolnot")

Related topics