tcl::mathop bitwise
The commands of the operators of bits: shifts, and, or, exclusive or, and not.
tcl::mathop::<< INTEGER SHIFT
tcl::mathop::>> INTEGER SHIFT
tcl::mathop::& ?INTEGER ...?
tcl::mathop::| ?INTEGER ...?
tcl::mathop::^ ?INTEGER ...?
tcl::mathop::~ INTEGER
Description
Each command works on integers of any size, as numbers in two's complement with an endless row of sign bits. A float is an error.
<<shifts INTEGER left by SHIFT bits, and the result grows with no limit:<< 1 64is 2 to the power of 64.>>shifts INTEGER right by SHIFT bits, with the sign bit:>> -8 1is -4.&gives the bitwise and of its arguments, and -1 with none.|gives the bitwise or of its arguments, and 0 with none.^gives the bitwise exclusive or of its arguments, and 0 with none.~gives the bitwise not of one integer:-INTEGER - 1.
A negative SHIFT is an error.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| INTEGER | integer | required | An integer of any size. |
| SHIFT | integer | required | The count of bits to shift, 0 or more. |
Returns
An integer.
Examples
% tcl::mathop::<< 1 3
8
% tcl::mathop::<< 1 64
18446744073709551616
% tcl::mathop::>> -8 1
-4
% tcl::mathop::>> 1 100
0
% tcl::mathop::& 12 10
8
% tcl::mathop::&
-1
% tcl::mathop::| 1 2 4
7
% tcl::mathop::^ 5 3
6
% tcl::mathop::~ 5
-6
Errors
% tcl::mathop::<< 1 -1
negative shift argument
-errorcode NONE
% tcl::mathop::<< 1
wrong # args: should be "tcl::mathop::<< integer shift"
-errorcode TCL WRONGARGS
% tcl::mathop::~ 1 2
wrong # args: should be "tcl::mathop::~ integer"
-errorcode TCL WRONGARGS
# Tcl 9.1 only
% tcl::mathop::~ 1.5
cannot use floating-point value "1.5" as operand of "~"
-errorcode ARITH DOMAIN {floating-point value}
# Tcl 8.6 only
% tcl::mathop::~ 1.5
can't use floating-point value as operand of "~"
-errorcode ARITH DOMAIN {floating-point value}
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1