1055 |
@node Bitwise Operations |
@node Bitwise Operations |
1056 |
@subsection Bitwise Operations |
@subsection Bitwise Operations |
1057 |
|
|
1058 |
@deffn {Scheme Procedure} logand n1 n2 |
For the following bitwise functions, negative numbers are treated as |
1059 |
|
infinite precision twos-complements. For instance @math{-6} is bits |
1060 |
|
@math{@dots{}111010}, with infinitely many ones on the left. It can |
1061 |
|
be seen that adding 6 (binary 110) to such a bit pattern gives all |
1062 |
|
zeros. |
1063 |
|
|
1064 |
|
@deffn {Scheme Procedure} logand n1 n2 @dots{} |
1065 |
Return the bitwise @sc{and} of the integer arguments. |
Return the bitwise @sc{and} of the integer arguments. |
1066 |
|
|
1067 |
@lisp |
@lisp |
1071 |
@end lisp |
@end lisp |
1072 |
@end deffn |
@end deffn |
1073 |
|
|
1074 |
@deffn {Scheme Procedure} logior n1 n2 |
@deffn {Scheme Procedure} logior n1 n2 @dots{} |
1075 |
Return the bitwise @sc{or} of the integer arguments. |
Return the bitwise @sc{or} of the integer arguments. |
1076 |
|
|
1077 |
@lisp |
@lisp |
1081 |
@end lisp |
@end lisp |
1082 |
@end deffn |
@end deffn |
1083 |
|
|
1084 |
@deffn {Scheme Procedure} logxor n1 n2 |
@deffn {Scheme Procedure} logxor n1 n2 @dots{} |
1085 |
Return the bitwise @sc{xor} of the integer arguments. A bit is |
Return the bitwise @sc{xor} of the integer arguments. A bit is |
1086 |
set in the result if it is set in an odd number of arguments. |
set in the result if it is set in an odd number of arguments. |
1087 |
|
|
1088 |
@lisp |
@lisp |
1089 |
(logxor) @result{} 0 |
(logxor) @result{} 0 |
1090 |
(logxor 7) @result{} 7 |
(logxor 7) @result{} 7 |
1131 |
|
|
1132 |
@deffn {Scheme Procedure} ash n cnt |
@deffn {Scheme Procedure} ash n cnt |
1133 |
@deffnx {C Function} scm_ash (n, cnt) |
@deffnx {C Function} scm_ash (n, cnt) |
1134 |
The function @code{ash} performs an arithmetic shift left by @var{cnt} |
Return @var{n} shifted left by @var{cnt} bits, or shifted right if |
1135 |
bits (or shift right, if @var{cnt} is negative). `Arithmetic' |
@var{cnt} is negative. This is an ``arithmetic'' shift. |
|
means that the function does not guarantee to keep the bit |
|
|
structure of @var{n}, but rather guarantees that the result |
|
|
will always be rounded towards minus infinity. Therefore, the |
|
|
results of @code{ash} and a corresponding bitwise shift will differ if |
|
|
@var{n} is negative. |
|
1136 |
|
|
1137 |
Formally, the function returns an integer equivalent to |
This is effectively a multiplication by @m{2^{cnt}, 2^@var{cnt}}, and |
1138 |
@code{(inexact->exact (floor (* @var{n} (expt 2 @var{cnt}))))}. |
when @var{cnt} is negative it's a division, rounded towards negative |
1139 |
|
infinity. (Note that this is not the same rounding as @code{quotient} |
1140 |
|
does.) |
1141 |
|
|
1142 |
|
With @var{n} viewed as an infinite precision twos complement, |
1143 |
|
@code{ash} means a left shift introducing zero bits, or a right shift |
1144 |
|
dropping bits. |
1145 |
|
|
1146 |
@lisp |
@lisp |
1147 |
(number->string (ash #b1 3) 2) @result{} "1000" |
(number->string (ash #b1 3) 2) @result{} "1000" |
1148 |
(number->string (ash #b1010 -1) 2) @result{} "101" |
(number->string (ash #b1010 -1) 2) @result{} "101" |
1149 |
|
|
1150 |
|
;; -23 is bits ...11101001, -6 is bits ...111010 |
1151 |
|
(ash -23 -2) @result{} -6 |
1152 |
@end lisp |
@end lisp |
1153 |
@end deffn |
@end deffn |
1154 |
|
|
1173 |
@deffnx {C Function} scm_integer_length (n) |
@deffnx {C Function} scm_integer_length (n) |
1174 |
Return the number of bits necessary to represent @var{n}. |
Return the number of bits necessary to represent @var{n}. |
1175 |
|
|
1176 |
|
For positive @var{n} this is how many bits to the most significant one |
1177 |
|
bit. For negative @var{n} it's how many bits to the most significant |
1178 |
|
zero bit in twos complement form. |
1179 |
|
|
1180 |
@lisp |
@lisp |
1181 |
(integer-length #b10101010) |
(integer-length #b10101010) @result{} 8 |
1182 |
@result{} 8 |
(integer-length #b1111) @result{} 4 |
1183 |
(integer-length 0) |
(integer-length 0) @result{} 0 |
1184 |
@result{} 0 |
(integer-length -1) @result{} 0 |
1185 |
(integer-length #b1111) |
(integer-length -256) @result{} 8 |
1186 |
@result{} 4 |
(integer-length -257) @result{} 9 |
1187 |
@end lisp |
@end lisp |
1188 |
@end deffn |
@end deffn |
1189 |
|
|