Scheme Programmer's Manual
NAME
bitwisenot  bitwise negation, one's complement
LIBRARY
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
SYNOPSIS
(bitwisenot ei)
(fxnot fx)
DESCRIPTION
Returns the bitwise negation of the exact integer
ei.
If Scheme integers were unsigned (i.e. only positive) then this could
be thought of as negating every bit in the integer. There is however a
subtle difference, having to do with the sign of the returned integer,
that makes such a description not quite accurate.
The fxnot procedure takes a fixnum as an argument and always
returns a fixnum (but see the ERRORS section below).
IMPLEMENTATION NOTES
 GNU Guile

In GNU Guile this procedure is faster than the equivalent fixnumonly
procedure
fxnot(3).
RETURN VALUES
Returns the exact integer object whose two's complement representation
is the one's complement of the two's complement representation of
ei.
EXAMPLES
;; There are no bits set in zero, so the returned value
;; has all bits set. Negative numbers notionally have all
;; bits set to the left of the most significant zero bit.
(number>string (bitwisenot #b0) 2)
=> "1"
;; Visually, it may not be obvious that the returned value
;; is the one'scomplement:
(number>string (bitwisenot #b11110000) 2)
=> "11110001"
;; Masking the value to the desired bitwidth will reveal
;; that the value is correct:
(number>string
(bitwiseand #xFFFF ; treat as 16bit unsigned
(bitwisenot #b11110000)) 2)
=> "1111111100001111"
;; The complement of the complement is the original number:
(bitwisenot 43) => 42
(bitwisenot 42) => 43
RATIONALE
bitwisenot
is one of the fundamental bitwise operators.
ERRORS
This procedure can raise exceptions with the following condition types:
 &assertion (R6RS)

The wrong number of arguments was passed or an argument was outside its domain.
 &implementationrestriction (R6RS)

The result is not representable as a fixnum. Raised for
(fxnot (leastfixnum)).
SEE ALSO
fxnot(3)
STANDARDS
R6RS,
SRFI60,
SRFI151
HISTORY
The history of bitwise complement can likely be traced to the first
binary computers that provided a NOT instruction, or to the
invention of binary arithmetic. In Scheme it appeared relatively late
with the (withdrawn) SRFI33 in 2003, and would not appear in an RnRS
document until R6RS in 2007.
AUTHORS
This page is part of the
schememanpages
project.
It includes materials from the RnRS documents.
More information can be found at
BUGS
Index
 NAME

 LIBRARY

 SYNOPSIS

 DESCRIPTION

 IMPLEMENTATION NOTES

 RETURN VALUES

 EXAMPLES

 RATIONALE

 ERRORS

 SEE ALSO

 STANDARDS

 HISTORY

 AUTHORS

 BUGS

Return to Main Contents