Skip To Content

~

Available with Spatial 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 on operator precedence, see operator precedence table. 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.

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.

Syntax

~ in_raster_or_constant
OperandExplanationData Type
in_raster_or_constant

The input to use in the Boolean Not operation.

Raster Layer | Constant

Return Value

NameExplanationData Type
out_raster

The output raster object.

The output values will be either 0 or 1.

Raster

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")

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute BooleanNot
outBooleanNot = ~ inRaster

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

Related topics