~ (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 there are multiple operators in an expression, the operators are not necessarily run in left-to-right order. The operator with the highest precedence value will be run first. For more information, see the operator precedence table in Work with operators in map algebra. Use parentheses to control the run 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 run first. To change the order in which the operators are run, use parentheses.

When multiple Relational or Boolean operators are used consecutively in a single expression, the expression may fail to run in some cases. To avoid, use appropriate parentheses in the expression so that the run order of the operators is explicitly defined. For more information, see Complex statement rules.

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