Bitwise operations: Difference between revisions
m →{{header|Beeswax}}: typo corrected, minor addition |
|||
Line 1,082: | Line 1,082: | ||
write(*,fmt2) 'or : ', a,' | ',b,' = ',ior(a, b),ior(a, b) |
write(*,fmt2) 'or : ', a,' | ',b,' = ',ior(a, b),ior(a, b) |
||
write(*,fmt2) 'xor : ', a,' ^ ',b,' = ',ieor(a, b),ieor(a, b) |
write(*,fmt2) 'xor : ', a,' ^ ',b,' = ',ieor(a, b),ieor(a, b) |
||
write(*,fmt2) 'lsh : ', a,' << ',b,' = ', |
write(*,fmt2) 'lsh : ', a,' << ',b,' = ',shiftl(a,b),shiftl(a,b) !since F2008, otherwise use ishft(a, abs(b)) |
||
write(*,fmt2) 'rsh : ', a,' >> ',b,' = ', |
write(*,fmt2) 'rsh : ', a,' >> ',b,' = ',shiftr(a,b),shiftr(a,b) !since F2008, otherwise use ishft(a, -abs(b)) |
||
write(*,fmt2) 'not : ', a,' ~ ',b,' = ',not(a),not(a) |
write(*,fmt2) 'not : ', a,' ~ ',b,' = ',not(a),not(a) |
||
write(*,fmt2) 'rot : ', a,' r ',b,' = ',ishftc(a,-abs(b)),ishftc(a,-abs(b)) |
write(*,fmt2) 'rot : ', a,' r ',b,' = ',ishftc(a,-abs(b)),ishftc(a,-abs(b)) |
Revision as of 13:18, 23 February 2016
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:
Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses
Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.
8051 Assembly
Integer one is assumed to be a, integer two assumed to be b. Each operation affects one or both operands and would not be used sequentially. The end result of each operation resides in a. The shift and rotate operations should likely push psw and pop psw because they affect the carry flag. <lang asm>; bitwise AND anl a, b
- bitwise OR
orl a, b
- bitwise XOR
xrl a, b
- bitwise NOT
cpl a
- left shift
inc b rrc a loop: rlc a clr c djnz b, loop
- right shift
inc b rlc a loop: rrc a clr c djnz b, loop
- arithmetic right shift
push 20 inc b rlc a mov 20.0, c loop: rrc a mov c, 20.0 djnz b, loop pop 20
- left rotate
inc b rr a loop: rl a djnz b, loop
- right rotate
inc b rl a loop: rr a djnz b, loop</lang>
ACL2
Unlisted operations are not available <lang Lisp>(defun bitwise (a b)
(list (logand a b) (logior a b) (logxor a b) (lognot a) (ash a b) (ash a (- b))))</lang>
ActionScript
ActionScript does not support bitwise rotations. <lang ActionScript>function bitwise(a:int, b:int):void { trace("And: ", a & b); trace("Or: ", a | b); trace("Xor: ", a ^ b); trace("Not: ", ~a); trace("Left Shift: ", a << b); trace("Right Shift(Arithmetic): ", a >> b); trace("Right Shift(Logical): ", a >>> b); }</lang>
Ada
The following program performs all required operations and prints the resulting values in base 2 for easy checking of the bit values.
<lang ada>with Ada.Text_Io; use Ada.Text_Io; with Interfaces; use Interfaces;
procedure Bitwise is subtype Byte is Unsigned_8; package Byte_Io is new Ada.Text_Io.Modular_Io(Byte); A : Byte := 255; B : Byte := 170; X : Byte := 128; N : Natural := 1;
begin Put_Line("A and B = "); Byte_Io.Put(Item => A and B, Base => 2); Put_Line("A or B = "); Byte_IO.Put(Item => A or B, Base => 2); Put_Line("A xor B = "); Byte_Io.Put(Item => A xor B, Base => 2); Put_Line("Not A = "); Byte_IO.Put(Item => not A, Base => 2); New_Line(2); Put_Line(Unsigned_8'Image(Shift_Left(X, N))); -- Left shift Put_Line(Unsigned_8'Image(Shift_Right(X, N))); -- Right shift Put_Line(Unsigned_8'Image(Shift_Right_Arithmetic(X, N))); -- Right Shift Arithmetic Put_Line(Unsigned_8'Image(Rotate_Left(X, N))); -- Left rotate Put_Line(Unsigned_8'Image(Rotate_Right(X, N))); -- Right rotate end bitwise;</lang>
Aikido
There is no rotate support built in to Aikido. <lang aikido>function bitwise(a, b){
println("a AND b: " + (a & b)) println("a OR b: "+ (a | b)) println("a XOR b: "+ (a ^ b)) println("NOT a: " + ~a) println("a << b: " + (a << b)) // left shift println("a >> b: " + (a >> b)) // arithmetic right shift println("a >>> b: " + (a >>> b)) // logical right shift
}</lang>
ALGOL 68
Aside from decimal, ALGOL 68 has 5 different alternative was of representing the number 170:
- 2r00000000000000000000000010101010, 4r0000000000002222, 8r00000000252, 16r000000aa
- and as an array of BOOL: FFFFFFFFFFFFFFFFFFFFFFFFTFTFTFTF
<lang algol68>main:(
PRIO SLC = 8, SRC = 8; # SLC and SRC are not built in, define and overload them here # OP SLC = (BITS b, INT rotate) BITS: b SHL rotate OR b SHR ( bits width - rotate ); OP SRC = (BITS b, INT rotate) BITS: b SHR rotate OR b SHL ( bits width - rotate ); # SRC and SRL are non-standard, but versions are built in to ALGOL 68R's standard prelude #
PRIO XOR = 2; OP XOR = (BITS p, q) BITS: p AND NOT q OR NOT p AND q; # XOR is non-standard, but a version is built in to ALGOL 68G's standard prelude #
# ALGOL 68 has 5 different ways of representing a BINary BITS - Bases: 2, 4, 8, 16 and flip/flop # FORMAT b5 = $"2r"2r32d," 4r"4r16d," 8r"8r11d," 16r"16r8d," "gl$; OP BBBBB = (BITS b)[]BITS: (b,b,b,b,b);
PROC bitwise = (BITS a, BITS b, INT shift)VOID: ( printf(( $" bits shorths: "gxgl$, bits shorths, "1 plus the number of extra SHORT BITS types", $" bits lengths: "gxgl$, bits lengths, "1 plus the number of extra LONG BITS types", $" max bits: "gl$, max bits, $" long max bits: "gl$, long max bits, $" long long max bits: "gl$, long long max bits, $" bits width: "gxgl$, bits width, "The number of CHAR required to display BITS", $" long bits width: "gxgl$, long bits width, "The number of CHAR required to display LONG BITS", $" long long bits width: "gxgl$, long long bits width, "The number of CHAR required to display LONG LONG BITS", $" bytes shorths: "gxgl$, bytes shorths, "1 plus the number of extra SHORT BYTES types", $" bytes lengths: "gxgl$, bits lengths, "1 plus the number of extra LONG BYTES types", $" bytes width: "gxgl$, bytes width, "The number of CHAR required to display BYTES", $" long bytes width: "gxgl$, long bytes width, "The number of CHAR required to display LONG BYTES" ));
printf(($" a: "f(b5)$, BBBBB a)); printf(($" b: "f(b5)$, BBBBB b)); printf(($" a AND b: "f(b5)$, BBBBB(a AND b))); printf(($" a OR b: "f(b5)$, BBBBB(a OR b))); printf(($" a XOR b: "f(b5)$, BBBBB(a XOR b))); printf(($" NOT b: "f(b5)$, BBBBB NOT a)); printf(($" a SHL "d": "f(b5)$, shift, BBBBB(a SHL shift))); printf(($" a SHR "d": "f(b5)$, shift, BBBBB(a SHR shift)));
printf(($" a SLC "d": "f(b5)$, shift, BBBBB(a SLC shift))); printf(($" a SRC "d": "f(b5)$, shift, BBBBB(a SRC shift)))
COMMENT with original ALGOL 68 character set;
printf(($" a AND b: "f(b5)$, BBBBB(a ∧ b))); printf(($" a OR b: "f(b5)$, BBBBB(a ∨ b))); printf(($" NOT b: "f(b5)$, BBBBB ¬ a)); printf(($" a SHL "d": "f(b5)$, shift, BBBBB(a ↑ shift))); printf(($" a SHR "d": "f(b5)$, shift, BBBBB(a ↓ shift)));
Also:
printf(($" a AND b: "f(b5)$, BBBBB(a /\ b))); printf(($" a OR b: "f(b5)$, BBBBB(a \/ b)));
COMMENT
);
bitwise(BIN 255, BIN 170, 5)
- or using alternate representations for 255 and 170 in BITS #
CO
bitwise(2r11111111,2r10101010,5); bitwise(4r3333,4r2222,5); bitwise(8r377,8r252,5); bitwise(16rff,16raa,5)
END CO )</lang> Output:
bits shorths: +1 1 plus the number of extra SHORT BITS types bits lengths: +3 1 plus the number of extra LONG BITS types max bits: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT long max bits: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT long long max bits: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT bits width: +32 The number of CHAR required to display BITS long bits width: +116 The number of CHAR required to display LONG BITS long long bits width: +232 The number of CHAR required to display LONG LONG BITS bytes shorths: +1 1 plus the number of extra SHORT BYTES types bytes lengths: +3 1 plus the number of extra LONG BYTES types bytes width: +32 The number of CHAR required to display BYTES long bytes width: +64 The number of CHAR required to display LONG BYTES a: 2r00000000000000000000000011111111 4r0000000000003333 8r00000000377 16r000000ff FFFFFFFFFFFFFFFFFFFFFFFFTTTTTTTT b: 2r00000000000000000000000010101010 4r0000000000002222 8r00000000252 16r000000aa FFFFFFFFFFFFFFFFFFFFFFFFTFTFTFTF a AND b: 2r00000000000000000000000010101010 4r0000000000002222 8r00000000252 16r000000aa FFFFFFFFFFFFFFFFFFFFFFFFTFTFTFTF a OR b: 2r00000000000000000000000011111111 4r0000000000003333 8r00000000377 16r000000ff FFFFFFFFFFFFFFFFFFFFFFFFTTTTTTTT a XOR b: 2r00000000000000000000000001010101 4r0000000000001111 8r00000000125 16r00000055 FFFFFFFFFFFFFFFFFFFFFFFFFTFTFTFT NOT b: 2r11111111111111111111111100000000 4r3333333333330000 8r37777777400 16rffffff00 TTTTTTTTTTTTTTTTTTTTTTTTFFFFFFFF a SHL 5: 2r00000000000000000001111111100000 4r0000000001333200 8r00000017740 16r00001fe0 FFFFFFFFFFFFFFFFFFFTTTTTTTTFFFFF a SHR 5: 2r00000000000000000000000000000111 4r0000000000000013 8r00000000007 16r00000007 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFTTT a SLC 5: 2r00000000000000000001111111100000 4r0000000001333200 8r00000017740 16r00001fe0 FFFFFFFFFFFFFFFFFFFTTTTTTTTFFFFF a SRC 5: 2r11111000000000000000000000000111 4r3320000000000013 8r37000000007 16rf8000007 TTTTTFFFFFFFFFFFFFFFFFFFFFFFFTTT
Note that an INT can be widened into BITS, and BITS can be widened into an array of BOOL. eg: <lang algol68># unpack (widen) some data back into an a BOOL array # INT i := 170; BITS j := BIN i; [bits width]BOOL k := j;
printf(($g", 8r"8r4d", "8(g)l$, i, j, k[bits width-8+1:]));
- now pack some data back into an INT #
k[bits width-8+1:] := (FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE); j := bits pack(k); i := ABS j;
printf(($g", 8r"8r4d", "8(g)l$, i, j, k[bits width-8+1:]))</lang> Output:
+170, 8r0252, TFTFTFTF +85, 8r0125, FTFTFTFT
AutoHotkey
<lang AutoHotkey>bitwise(3, 4) bitwise(a, b) {
MsgBox % "a and b: " . a & b MsgBox % "a or b: " . a | b MsgBox % "a xor b: " . a ^ b MsgBox % "not a: " . ~a ; treated as unsigned integer MsgBox % "a << b: " . a << b ; left shift MsgBox % "a >> b: " . a >> b ; arithmetic right shift
}</lang>
AWK
Standard awk does not have bitwise operators. Gawk has built-in functions for many bitwise operations. No rotation of bits.
<lang awk>BEGIN {
n = 11 p = 1 print n " or " p " = " or(n,p) print n " and " p " = " and(n,p) print n " xor " p " = " xor(n,p) print n " << " p " = " lshift(n, p) # left shift print n " >> " p " = " rshift(n, p) # right shift printf "not %d = 0x%x\n", n, compl(n) # bitwise complement
}</lang>
OpenBSD /usr/bin/awk
(a variant of nawk) has these same functions, with a few differences. Gawk uses 53-bit unsigned integers, but OpenBSD awk uses 32-bit signed integers. Therefore Gawk prints not 11 = 0x1ffffffffffff4
, but OpenBSD awk prints not 11 = 0xfffffff4
.
Axe
<lang axe>Lbl BITS r₁→A r₂→B Disp "AND:",A·B▶Dec,i Disp "OR:",AᕀB▶Dec,i Disp "XOR:",A▫B▶Dec,i Disp "NOT:",not(A)ʳ▶Dec,i .No language support for shifts or rotations Return</lang>
Note that the symbols for AND, OR, and XOR are the stat plot marks near the bottom of the Catalog.
Babel
<lang babel>((main { (5 9) foo ! })
(foo {
({cand} {cor} {cnor} {cxor} {cxnor} {cushl} {cushr} {cashr} {curol} {curor}) { <- dup give -> eval %x nl <<} each give zap cnot %x nl <<}))</lang>
This program produces the following output:
<lang babel>1 d fffffff7 c fffffff3 a00 0 0 a00 2800000 fffffffa</lang>
BASIC
QuickBasic does not have shift or rotate operations defined. Here are the logical operations: <lang qbasic>SUB bitwise (a, b)
PRINT a AND b PRINT a OR b PRINT a XOR b PRINT NOT a
END SUB</lang>
FreeBASIC does not have rotate operators. Shift Right operator performs arithmetic shift if the left value is signed number and logical shift if the left value is unsigned number. <lang freebasic>SUB bitwise (a AS Integer, b AS Integer)
DIM u AS UInteger
PRINT "a AND b = "; a AND b PRINT "a OR b = "; a OR b PRINT "a XOR b = "; a XOR b PRINT "NOT a = "; NOT a PRINT "a SHL b = "; a SHL b PRINT "a SHR b (arithmetic) = "; a SHR b u = a PRINT "a SHR b (logical) = "; u SHR b
END SUB</lang>
BASIC256
<lang BASIC256># bitwise operators - floating point numbers will be cast to integer a = 0b00010001 b = 0b11110000 print a print int(a * 2) # right shift (multiply by 2) print a \ 2 # left shift (integer divide by 2) print a | b # bitwise or on two integer values print a & b # bitwise or on two integer values</lang>
Batch File
The SET command with the /A option supports arithmetic and bit operations on signed 8 byte integers.
The SET /? documentation claims it supports logical shift operations, but in reality it performs an arithmetic right shift.
The following script (bitops.bat) not only demonstrates the basic bit operations, it also uses bit operations to convert each integral value into a string of 32 binary digits. <lang dos> @echo off setlocal set /a "a=%~1, b=%~2" call :num2bin %a% aStr call :num2bin %b% bStr
- AND
set /a "val=a&b" call :display "%a% AND %b%" %val% %aStr% %bStr%
- OR
set /a "val=a|b" call :display "%a% OR %b%" %val% %aStr% %bStr%
- XOR
set /a "val=a^b" call :display "%a% XOR %b%" %val% %aStr% %bStr%
- NOT
set /a "val=~a" call :display "NOT %a%" %val% %aStr%
- LEFT SHIFT
set /a "val=a<<b" call :display "%a% Left Shift %b%" %val% %aStr%
- ARITHMETIC RIGHT SHIFT
set /a "val=a>>b" call :display "%a% Arithmetic Right Shift %b%" %val% %aStr%
- The remaining operations do not have native support
- The implementations use additional operators
- %% = mod
- ! = logical negation where !(zero)=1 and !(non-zero)=0
- * = multiplication
- - = subtraction
- LOGICAL RIGHT SHIFT (No native support)
set /a "val=(a>>b)&~((0x80000000>>b-1)*!!b)" call :display "%a% Logical Right Shift %b%" %val% %aStr%
- ROTATE LEFT (No native support)
set /a "val=(a<<b%%32) | (a>>32-b%%32)&~((0x80000000>>31-b%%32)*!!(32-b%%32))" call :display "%a% Rotate Left %b%" %val% %aStr%
- ROTATE RIGHT (No native support)
set /a "val=(a<<32-b%%32) | (a>>b%%32)&~((0x80000000>>b%%32-1)*!!(b%%32)) " call :display "%a% Rotate Right %b%" %val% %aStr%
exit /b
- display op result aStr [bStr]
echo( echo %~1 = %2 echo %3 if "%4" neq "" echo %4 call :num2bin %2 exit /b
- num2bin IntVal [RtnVar]
setlocal enableDelayedExpansion set n=%~1 set rtn= for /l %%b in (0,1,31) do ( set /a "d=n&1, n>>=1" set rtn=!d!!rtn! ) (endlocal & rem -- return values if "%~2" neq "" (set %~2=%rtn%) else echo %rtn% )
exit /b </lang>
Sample output
>bitops 0x800000FE 7 -2147483394 AND 7 = 6 10000000000000000000000011111110 00000000000000000000000000000111 00000000000000000000000000000110 -2147483394 OR 7 = -2147483393 10000000000000000000000011111110 00000000000000000000000000000111 10000000000000000000000011111111 -2147483394 XOR 7 = -2147483399 10000000000000000000000011111110 00000000000000000000000000000111 10000000000000000000000011111001 NOT -2147483394 = 2147483393 10000000000000000000000011111110 01111111111111111111111100000001 -2147483394 Left Shift 7 = 32512 10000000000000000000000011111110 00000000000000000111111100000000 -2147483394 Arithmetic Right Shift 7 = -16777215 10000000000000000000000011111110 11111111000000000000000000000001 -2147483394 Logical Right Shift 7 = 16777217 10000000000000000000000011111110 00000001000000000000000000000001 -2147483394 Rotate Left 7 = 32576 10000000000000000000000011111110 00000000000000000111111101000000 -2147483394 Rotate Right 7 = -50331647 10000000000000000000000011111110 11111101000000000000000000000001
BBC BASIC
<lang bbcbasic> number1% = &89ABCDEF
number2% = 8 PRINT ~ number1% AND number2% : REM bitwise AND PRINT ~ number1% OR number2% : REM bitwise OR PRINT ~ number1% EOR number2% : REM bitwise exclusive-OR PRINT ~ NOT number1% : REM bitwise NOT PRINT ~ number1% << number2% : REM left shift PRINT ~ number1% >>> number2% : REM right shift (logical) PRINT ~ number1% >> number2% : REM right shift (arithmetic) PRINT ~ (number1% << number2%) OR (number1% >>> (32-number2%)) : REM left rotate PRINT ~ (number1% >>> number2%) OR (number1% << (32-number2%)) : REM right rotate</lang>
Beeswax
<lang beeswax>#eX~T~T_#
- >N{` AND `~{~` = `&{Nz1~3J
UXe#
- >{` OR `~{~` = `|{Nz1~5J
UXe#
- >{` XOR `~{~` = `${Nz1~7J
UXe#
- >`NOT `{` = `!{Nz1~9J
UXe#
- >{` << `~{~` = `({Nz1~9PPJ
UXe#
- >{` >>> `~{~` = `){` (logical shift right)`N7F+M~1~J
UXe#
- >{` ROL `~{~` = `[{N7F+P~1~J
UXe#
- >{` ROR `~{~` = `]{NN8F+P~1~J
UXe#
- >`Arithmetic shift right is not originally implemented in beeswax.`N q
qN`,noitagen yb dezilaer eb nac srebmun evitagen rof RSA ,yllacinhcet tuB`N<
- >`logical shift right, and negating the result again:`NN7F++~1~J
UXe# #>e#
#>~1~[&'pUX{` >> `~{~` = `){` , interpreted as (positive) signed Int64 number (MSB=0), equivalent to >>>`NN; ### >UX`-`!P{M!` >> `~{~` = `!)!`-`M!{` , interpreted as (negative) signed Int64 number (MSB=1)`NN; #>e#</lang>
Example: <lang Julia> julia> beeswax("Bitops.bswx",0,0.0,Int(20000)) i9223653511831486512 i48
9223653511831486512 AND 48 = 48 9223653511831486512 OR 48 = 9223653511831486512 9223653511831486512 XOR 48 = 9223653511831486464 NOT 9223653511831486512 = 9223090561878065103 9223653511831486512 << 48 = 13510798882111488 9223653511831486512 >>> 48 = 32769 (logical shift right) 9223653511831486512 ROL 48 = 13651540665434112 9223653511831486512 ROR 48 = 3178497
Arithmetic shift right is not originally implemented in beeswax.
But technically, ASR for negative numbers can be realized by negation, logical shift right, and negating the result again:
-9223090561878065104 >> 48 = -32767 , interpreted as (negative) signed Int64 number (MSB=1)</lang>
The natural number range for beeswax is unsigned Int64, but it is easy to implement signed Int64 by realizing negative numbers by their 2’s complements or interpreting numbers as negative if their MSB is 1, as shown in the example above.
Arithmetic shift right is not originally implemented in beeswax because it does not make sense for unsigned integers, but for negative numbers, it can be realized easily with <lang beeswax>A>>B = NOT(NOT(A)>>>B)</lang> as demonstrated above.
In beeswax, rotate left (ROL) and rotate right (ROT) operators are implemented using modulo 64, so rotations by more than 63 bits wrap around:
<lang beeswax>A ROL B = A<<(B%64)+A>>>(64-B%64) A ROR B = A>>>(B%64)+A<<(64-B%64)</lang>
C
<lang c>void bitwise(int a, int b) {
printf("a and b: %d\n", a & b); printf("a or b: %d\n", a | b); printf("a xor b: %d\n", a ^ b); printf("not a: %d\n", ~a); printf("a << n: %d\n", a << b); /* left shift */ printf("a >> n: %d\n", a >> b); /* on most platforms: arithmetic right shift */ /* convert the signed integer into unsigned, so it will perform logical shift */ unsigned int c = a; printf("c >> b: %d\n", c >> b); /* logical right shift */ /* there are no rotation operators in C */ return 0;
}</lang>
To rotate an integer, you can combine a left shift and a right shift:
<lang C>/* rotate x to the right by s bits */
unsigned int rotr(unsigned int x, unsigned int s)
{
return (x >> s) | (x << 32 - s);
}</lang>With a smart enough compiler, the above actually compiles into a single machine bit rotate instruction when possible. E.g. gcc -S
on IA32 produced following assembly code:<lang Assembly>rotr:
movl 4(%esp), %eax ; arg1: x movl 8(%esp), %ecx ; arg2: s rorl %cl, %eax ; right rotate x by s ret</lang>
C++
<lang cpp>#include <iostream>
void bitwise(int a, int b) {
std::cout << "a and b: " << (a & b) << '\n'; // Note: parentheses are needed because & has lower precedence than << std::cout << "a or b: " << (a | b) << '\n'; std::cout << "a xor b: " << (a ^ b) << '\n'; std::cout << "not a: " << ~a << '\n'; std::cout << "a shl b: " << (a << b) << '\n'; // Note: "<<" is used both for output and for left shift std::cout << "a shr b: " << (a >> b) << '\n'; // typically arithmetic right shift, but not guaranteed unsigned int c = a; std::cout << "c sra b: " << (c >> b) << '\n'; // logical right shift (guaranteed) // there are no rotation operators in C++
}</lang>
C#
<lang csharp>static void bitwise(int a, int b)
{ Console.WriteLine("a and b is {0}", a & b); Console.WriteLine("a or b is {0}", a | b); Console.WriteLine("a xor b is {0}", a ^ b); Console.WriteLine("not a is {0}", ~a); Console.WriteLine("a lshift b is {0}", a << b); Console.WriteLine("a arshift b is {0}", a >> b); // When the left operand of the >> operator is of a signed integral type, // the operator performs an arithmetic shift right uint c = (uint)a; Console.WriteLine("c rshift b is {0}", c >> b); // When the left operand of the >> operator is of an unsigned integral type, // the operator performs a logical shift right // there are no rotation operators in C# }</lang>
Clojure
<lang lisp>(bit-and x y) (bit-or x y) (bit-xor x y) (bit-not x) (bit-shift-left x n) (bit-shift-right x n)
- There is no built-in for rotation.</lang>
COBOL
Results are displayed in decimal. <lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. bitwise-ops.
DATA DIVISION. LOCAL-STORAGE SECTION. 01 a PIC 1(32) USAGE BIT. 01 b PIC 1(32) USAGE BIT.
01 result PIC 1(32) USAGE BIT. 01 result-disp REDEFINES result PIC S9(9) COMP.
LINKAGE SECTION. 01 a-int USAGE BINARY-LONG. 01 b-int USAGE BINARY-LONG.
PROCEDURE DIVISION USING a-int, b-int. MOVE FUNCTION BOOLEAN-OF-INTEGER(a-int, 32) TO a MOVE FUNCTION BOOLEAN-OF-INTEGER(b-int, 32) TO b
COMPUTE result = a B-AND b DISPLAY "a and b is " result-disp
COMPUTE result = a B-OR b DISPLAY "a or b is " result-disp
COMPUTE result = B-NOT a DISPLAY "Not a is " result-disp
COMPUTE result = a B-XOR b DISPLAY "a exclusive-or b is " result-disp
*> COBOL does not have shift or rotation operators.
GOBACK .</lang>
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. mf-bitwise-ops. DATA DIVISION. LOCAL-STORAGE SECTION. 01 result USAGE BINARY-LONG.
78 arg-len VALUE LENGTH OF result. LINKAGE SECTION. 01 a USAGE BINARY-LONG. 01 b USAGE BINARY-LONG. PROCEDURE DIVISION USING a, b. main-line. MOVE b TO result CALL "CBL_AND" USING a, result, VALUE arg-len DISPLAY "a and b is " result MOVE b TO result CALL "CBL_OR" USING a, result, VALUE arg-len DISPLAY "a or b is " result MOVE a TO result CALL "CBL_NOT" USING result, VALUE arg-len DISPLAY "Not a is " result MOVE b TO result CALL "CBL_XOR" USING a, result, VALUE arg-len DISPLAY "a exclusive-or b is " result MOVE b TO result CALL "CBL_EQ" USING a, result, VALUE arg-len DISPLAY "Logical equivalence of a and b is " result MOVE b TO result CALL "CBL_IMP" USING a, result, VALUE arg-len DISPLAY "Logical implication of a and b is " result
GOBACK .</lang>
CoffeeScript
CoffeeScript provides sugar for some JavaScript operators, but the bitwise operators are taken directly from JS. See more here: http://coffeescript.org/#operators
<lang coffeescript> f = (a, b) ->
p "and", a & b p "or", a | b p "xor", a ^ b p "not", ~a p "<<", a << b p ">>", a >> b # no rotation shifts that I know of
p = (label, n) -> console.log label, n
f(10,2) </lang>
output <lang> > coffee foo.coffee and 2 or 10 xor 8 not -11 << 40 >> 2 </lang>
Common Lisp
<lang lisp>(defun bitwise (a b)
(print (logand a b)) ; AND (print (logior a b)) ; OR ("ior" = inclusive or) (print (logxor a b)) ; XOR (print (lognot a)) ; NOT (print (ash a b)) ; arithmetic left shift (positive 2nd arg) (print (ash a (- b))) ; arithmetic right shift (negative 2nd arg) ; no logical shift
)</lang>
Left and right logical shift may be implemented by the following functions:
<lang lisp> (defun shl (x width bits)
"Compute bitwise left shift of x by 'bits' bits, represented on 'width' bits" (logand (ash x bits) (1- (ash 1 width))))
(defun shr (x width bits)
"Compute bitwise right shift of x by 'bits' bits, represented on 'width' bits" (logand (ash x (- bits)) (1- (ash 1 width))))
</lang>
Left and right rotation may be implemented by the following functions:
<lang lisp> (defun rotl (x width bits)
"Compute bitwise left rotation of x by 'bits' bits, represented on 'width' bits" (logior (logand (ash x (mod bits width)) (1- (ash 1 width))) (logand (ash x (- (- width (mod bits width)))) (1- (ash 1 width)))))
(defun rotr (x width bits)
"Compute bitwise right rotation of x by 'bits' bits, represented on 'width' bits" (logior (logand (ash x (- (mod bits width))) (1- (ash 1 width))) (logand (ash x (- width (mod bits width))) (1- (ash 1 width)))))
</lang>
D
<lang d>T rot(T)(in T x, in int shift) pure nothrow @nogc {
return (x >>> shift) | (x << (T.sizeof * 8 - shift));
}
void testBit(in int a, in int b) {
import std.stdio; writefln("Input: a = %d, b = %d", a, b); writefln("AND : %8b & %08b = %032b (%4d)", a, b, a & b, a & b); writefln(" OR : %8b | %08b = %032b (%4d)", a, b, a | b, a | b); writefln("XOR : %8b ^ %08b = %032b (%4d)", a, b, a ^ b, a ^ b); writefln("LSH : %8b << %08b = %032b (%4d)", a, b, a << b, a << b); writefln("RSH : %8b >> %08b = %032b (%4d)", a, b, a >> b, a >> b); writefln("NOT : %8s ~ %08b = %032b (%4d)", "", a, ~a, ~a); writefln("ROT : rot(%8b, %d) = %032b (%4d)", a, b, rot(a, b), rot(a, b));
}
void main() {
immutable int a = 0b_1111_1111; // bit literal 255 immutable int b = 0b_0000_0010; // bit literal 2
testBit(a, b);
}</lang>
- Output:
Input: a = 255, b = 2 AND : 11111111 & 00000010 = 00000000000000000000000000000010 ( 2) OR : 11111111 | 00000010 = 00000000000000000000000011111111 ( 255) XOR : 11111111 ^ 00000010 = 00000000000000000000000011111101 ( 253) LSH : 11111111 << 00000010 = 00000000000000000000001111111100 (1020) RSH : 11111111 >> 00000010 = 00000000000000000000000000111111 ( 63) NOT : ~ 11111111 = 11111111111111111111111100000000 (-256) ROT : rot(11111111, 2) = 11000000000000000000000000111111 (-1073741761)
Compilers are usually able to optimize the code pattern of the rot function to one CPU instruction plus loads. The DMD compiler too performs such optimization.
Delphi
<lang Delphi>program Bitwise;
{$APPTYPE CONSOLE}
begin
Writeln('2 and 3 = ', 2 and 3); Writeln('2 or 3 = ', 2 or 3); Writeln('2 xor 3 = ', 2 xor 3); Writeln('not 2 = ', not 2); Writeln('2 shl 3 = ', 2 shl 3); Writeln('2 shr 3 = ', 2 shr 3);
// there are no built-in rotation operators in Delphi
Readln;
end.</lang>
DWScript
<lang Delphi>PrintLn('2 and 3 = '+IntToStr(2 and 3)); PrintLn('2 or 3 = '+IntToStr(2 or 3)); PrintLn('2 xor 3 = '+IntToStr(2 xor 3)); PrintLn('not 2 = '+IntToStr(not 2)); PrintLn('2 shl 3 = '+IntToStr(2 shl 3)); PrintLn('2 shr 3 = '+IntToStr(2 shr 3));</lang>
E
E provides arbitrary-size integers, so there is no distinct arithmetic and logical shift right. E does not provide bit rotate operations.
<lang e>def bitwise(a :int, b :int) {
println(`Bitwise operations: a AND b: ${a & b} a OR b: ${a | b} a XOR b: ${a ^ b} NOT a: " + ${~a} a left shift b: ${a << b} a right shift b: ${a >> b}
`) }</lang>
ECL
<lang ECL> BitwiseOperations(INTEGER A, INTEGER B) := FUNCTION
BitAND := A & B; BitOR := A | B; BitXOR := A ^ B; BitNOT := BNOT A; BitSL := A << B; BitSR := A >> B; DS := DATASET([{A,B,'Bitwise AND:',BitAND},
{A,B,'Bitwise OR:',BitOR}, {A,B,'Bitwise XOR',BitXOR}, {A,B,'Bitwise NOT A:',BitNOT}, {A,B,'ShiftLeft A:',BitSL}, {A,B,'ShiftRight A:',BitSR}], {INTEGER AVal,INTEGER BVal,STRING15 valuetype,INTEGER val});
RETURN DS;
END;
BitwiseOperations(255,5); //right arithmetic shift, left and right rotate not implemented /*
OUTPUT: 255 5 Bitwise AND: 5 255 5 Bitwise OR: 255 255 5 Bitwise XOR 250 255 5 Bitwise NOT A: -256 255 5 ShiftLeft A: 8160 255 5 ShiftRight A: 7
- /
</lang>
Elena
<lang elena>#define system.
- define extensions.
- class(extension) testOp
{
#method bitwiseTest : y [ console writeLine:self:" and ":y:" = ":(self and:y). console writeLine:self:" or ":y:" = ":(self or:y). console writeLine:self:" xor ":y:" = ":(self xor:y). console writeLine:"not ":self:" = ":(self not). console writeLine:self:" shr ":y:" = ":(self shift &index:y). console writeLine:self:" shl ":y:" = ":(self shift &index:(y negative)). ]
}
- symbol program =
[
console readLine:(Integer new) bitwiseTest:(console readLine:(Integer new)).
].</lang>
- Output:
255 and 2 = 2 255 or 2 = 255 255 xor 2 = 253 not 255 = -256 255 shr 2 = 63 255 shl 2 = 1020
Elixir
<lang elixir>defmodule Bitwise_operation do
use Bitwise def test(a \\ 255, b \\ 170, c \\ 2) do IO.puts "Bitwise function:" IO.puts "band(#{a}, #{b}) = #{band(a, b)}" IO.puts "bor(#{a}, #{b}) = #{bor(a, b)}" IO.puts "bxor(#{a}, #{b}) = #{bxor(a, b)}" IO.puts "bnot(#{a}) = #{bnot(a)}" IO.puts "bsl(#{a}, #{c}) = #{bsl(a, c)}" IO.puts "bsr(#{a}, #{c}) = #{bsr(a, c)}" IO.puts "\nBitwise as operator:" IO.puts "#{a} &&& #{b} = #{a &&& b}" IO.puts "#{a} ||| #{b} = #{a ||| b}" IO.puts "#{a} ^^^ #{b} = #{a ^^^ b}" IO.puts "~~~#{a} = #{~~~a}" IO.puts "#{a} <<< #{c} = #{a <<< c}" IO.puts "#{a} >>> #{c} = #{a >>> c}" end
end
Bitwise_operation.test</lang>
- Output:
Bitwise function: band(255, 170) = 170 bor(255, 170) = 255 bxor(255, 170) = 85 bnot(255) = -256 bsl(255, 2) = 1020 bsr(255, 2) = 63 Bitwise as operator: 255 &&& 170 = 170 255 ||| 170 = 255 255 ^^^ 170 = 85 ~~~255 = -256 255 <<< 2 = 1020 255 >>> 2 = 63
Erlang
All these operations are built-in functions except right arithmetic shift, left rotate, and right rotate.
<lang erlang> -module(bitwise_operations).
-export([test/0]).
test() ->
A = 255, B = 170, io:format("~p band ~p = ~p\n",[A,B,A band B]), io:format("~p bor ~p = ~p\n",[A,B,A bor B]), io:format("~p bxor ~p = ~p\n",[A,B,A bxor B]), io:format("not ~p = ~p\n",[A,bnot A]), io:format("~p bsl ~p = ~p\n",[A,B,A bsl B]), io:format("~p bsr ~p = ~p\n",[A,B,A bsr B]).
</lang>
outputs: <lang erlang> 255 band 170 = 170 255 bor 170 = 255 255 bxor 170 = 85 not 255 = -256 255 bsl 170 = 381627307539845370001346183518875822092557105621893120 255 bsr 170 = 0 </lang>
F#
<lang fsharp>let bitwise a b =
printfn "a and b: %d" (a &&& b) printfn "a or b: %d" (a ||| b) printfn "a xor b: %d" (a ^^^ b) printfn "not a: %d" (~~~a) printfn "a shl b: %d" (a <<< b) printfn "a shr b: %d" (a >>> b) // arithmetic shift printfn "a shr b: %d" ((uint32 a) >>> b) // logical shift // No rotation operators.</lang>
Factor
<lang factor>"a=" "b=" [ write readln string>number ] bi@ {
[ bitand "a AND b: " write . ] [ bitor "a OR b: " write . ] [ bitxor "a XOR b: " write . ] [ drop bitnot "NOT a: " write . ] [ abs shift "a asl b: " write . ] [ neg shift "a asr b: " write . ]
} 2cleave</lang>
outputs: <lang factor>a=255 b=5 a AND b: 5 a OR b: 255 a XOR b: 250 NOT a: -256 a asl b: 8160 a asr b: 7</lang> Currently rotation and logical shifts are not implemented.
FALSE
Only AND, OR, and NOT are available. <lang false>10 3 \$@$@$@$@\ { 3 copies } "a & b = "&." a | b = "|." ~a = "%~." "</lang>
Forth
<lang forth>: arshift 0 ?do 2/ loop ; \ 2/ is an arithmetic shift right by one bit (2* shifts left one bit)
- bitwise ( a b -- )
cr ." a = " over . ." b = " dup . cr ." a and b = " 2dup and . cr ." a or b = " 2dup or . cr ." a xor b = " 2dup xor . cr ." not a = " over invert . cr ." a shl b = " 2dup lshift . cr ." a shr b = " 2dup rshift . cr ." a ashr b = " 2dup arshift . 2drop ;</lang>
Rotation is not standard, but may be provided in particular Forth implementations, or as an assembly instruction in CODE words.
Fortran
In ISO Fortran 90 and later the following BIT INTRINSIC functions are defined: <lang fortran>integer :: i, j = -1, k = 42 logical :: a
i = bit_size(j) ! returns the number of bits in the given INTEGER variable
! bitwise boolean operations on integers i = iand(k, j) ! returns bitwise AND of K and J i = ior(k, j) ! returns bitwise OR of K and J i = ieor(k, j) ! returns bitwise EXCLUSIVE OR of K and J i = not(j) ! returns bitwise NOT of J
! single-bit integer/logical operations (bit positions are zero-based) a = btest(i, 4) ! returns logical .TRUE. if bit position 4 of I is 1, .FALSE. if 0 i = ibclr(k, 8) ! returns value of K with 8th bit position "cleared" (set to 0) i = ibset(k, 13) ! returns value of K with 13th bit position "set" (set to 1)
! multi-bit integer operations i = ishft(k, j) ! returns value of K shifted by J bit positions, with ZERO fill
! (right shift if J < 0 and left shift if J > 0).
i = ishftc(k, j) ! returns value of K shifted CIRCULARLY by J bit positions
! (right circular shift if J < 0 and left if J > 0)
i = ishftc(k, j, 20) ! returns value as before except that ONLY the 20 lowest order
! (rightmost) bits are circularly shifted
i = ibits(k, 7, 8) ! extracts 8 contiguous bits from K starting at position 7 and
! returns them as the rightmost bits of an otherwise ! zero-filled integer. For non-negative K this is ! arithmetically equivalent to: MOD((K / 2**7), 2**8)</lang>
The following INTRINSIC ELEMENTAL SUBROUTINE is also defined: <lang fortran> call mvbits(k, 2, 4, j, 0) ! copy a sequence of 4 bits from k starting at bit 2 into j starting at bit 0</lang>
<lang fortran> program bits_rosetta implicit none
call bitwise(14,3)
contains
subroutine bitwise(a,b) implicit none
integer, intent(in):: a,b character(len=*), parameter :: fmt1 = '(2(a,i10))' character(len=*),parameter :: fmt2 = '(3(a,b32.32),i20)'
write(*,fmt1) 'input a=',a,' b=',b write(*,fmt2) 'and : ', a,' & ',b,' = ',iand(a, b),iand(a, b) write(*,fmt2) 'or : ', a,' | ',b,' = ',ior(a, b),ior(a, b) write(*,fmt2) 'xor : ', a,' ^ ',b,' = ',ieor(a, b),ieor(a, b) write(*,fmt2) 'lsh : ', a,' << ',b,' = ',shiftl(a,b),shiftl(a,b) !since F2008, otherwise use ishft(a, abs(b)) write(*,fmt2) 'rsh : ', a,' >> ',b,' = ',shiftr(a,b),shiftr(a,b) !since F2008, otherwise use ishft(a, -abs(b)) write(*,fmt2) 'not : ', a,' ~ ',b,' = ',not(a),not(a) write(*,fmt2) 'rot : ', a,' r ',b,' = ',ishftc(a,-abs(b)),ishftc(a,-abs(b))
end subroutine bitwise
end program bits_rosetta </lang> Output <lang> Input a= 14 b= 3 AND : 00000000000000000000000000001110 & 00000000000000000000000000000011 = 00000000000000000000000000000010 2 OR : 00000000000000000000000000001110 | 00000000000000000000000000000011 = 00000000000000000000000000001111 15 XOR : 00000000000000000000000000001110 ^ 00000000000000000000000000000011 = 00000000000000000000000000001101 13 LSH : 00000000000000000000000000001110 << 00000000000000000000000000000011 = 00000000000000000000000001110000 112 RSH : 00000000000000000000000000001110 >> 00000000000000000000000000000011 = 00000000000000000000000000000001 1 NOT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11111111111111111111111111110001 -15 ROT : 00000000000000000000000000001110 ~ 00000000000000000000000000000011 = 11000000000000000000000000000001 -1073741823 </lang>
Go
<lang go>package main
import "fmt"
func bitwise(a, b int16) { fmt.Printf("a: %016b\n", uint16(a)) fmt.Printf("b: %016b\n", uint16(b))
// Bitwise logical operations fmt.Printf("and: %016b\n", uint16(a&b)) fmt.Printf("or: %016b\n", uint16(a|b)) fmt.Printf("xor: %016b\n", uint16(a^b)) fmt.Printf("not: %016b\n", uint16(^a))
if b < 0 { fmt.Println("Right operand is negative, but all shifts require an unsigned right operand (shift distance).") return } ua := uint16(a) ub := uint32(b)
// Logical shifts (unsigned left operand) fmt.Printf("shl: %016b\n", uint16(ua<<ub)) fmt.Printf("shr: %016b\n", uint16(ua>>ub))
// Arithmetic shifts (signed left operand) fmt.Printf("las: %016b\n", uint16(a<<ub)) fmt.Printf("ras: %016b\n", uint16(a>>ub))
// Rotations fmt.Printf("rol: %016b\n", uint16(a<<ub|int16(uint16(a)>>(16-ub)))) fmt.Printf("ror: %016b\n", uint16(int16(uint16(a)>>ub)|a<<(16-ub))) }
func main() { var a, b int16 = -460, 6 bitwise(a, b) }</lang> Output:
a: 1111111000110100 b: 0000000000000110 and: 0000000000000100 or: 1111111000110110 xor: 1111111000110010 not: 0000000111001011 shl: 1000110100000000 shr: 0000001111111000 las: 1000110100000000 ras: 1111111111111000 rol: 1000110100111111 ror: 1101001111111000
Groovy
<lang groovy>def bitwise = { a, b ->
println """
a & b = ${a} & ${b} = ${a & b} a | b = ${a} | ${b} = ${a | b} a ^ b = ${a} ^ ${b} = ${a ^ b} ~ a = ~ ${a} = ${~ a} a << b = ${a} << ${b} = ${a << b} a >> b = ${a} >> ${b} = ${a >> b} arithmetic (sign-preserving) shift a >>> b = ${a} >>> ${b} = ${a >>> b} logical (zero-filling) shift """ }</lang>
Program: <lang groovy>bitwise(-15,3)</lang>
Output:
a & b = -15 & 3 = 1 a | b = -15 | 3 = -13 a ^ b = -15 ^ 3 = -14 ~ a = ~ -15 = 14 a << b = -15 << 3 = -120 a >> b = -15 >> 3 = -2 arithmetic (sign-preserving) shift a >>> b = -15 >>> 3 = 536870910 logical (zero-filling) shift
Haskell
The operations in Data.Bits work on Int, Integer, and any of the sized integer and word types. <lang haskell>import Data.Bits
bitwise :: Int -> Int -> IO () bitwise a b = do
print $ a .&. b print $ a .|. b print $ a `xor` b print $ complement a print $ shiftL a b -- left shift print $ shiftR a b -- arithmetic right shift print $ shift a b -- You can also use the "unified" shift function; positive is for left shift, negative is for right shift print $ shift a (-b) print $ rotateL a b -- rotate left print $ rotateR a b -- rotate right print $ rotate a b -- You can also use the "unified" rotate function; positive is for left rotate, negative is for right rotate print $ rotate a (-b)
main = bitwise 255 170</lang> If you were shifting Words (unsigned integers) instead of Ints, then the shift would be automatically logical shifts:
import Data.Word print $ shiftL (-1 :: Word) 1 print $ shiftR (-1 :: Word) 1
HicEst
There is no rotate and no shift support built in to HicEst <lang hicest>i = IAND(k, j) i = IOR( k, j) i = IEOR(k, j) i = NOT( k )</lang>
Icon and Unicon
<lang Icon>procedure main() bitdemo(255,2) bitdemo(-15,3) end
procedure bitdemo(i,i2)
write() demowrite("i",i) demowrite("i2",i2) demowrite("complement i",icom(i)) demowrite("i or i2",ior(i,i2)) demowrite("i and i2",iand(i,i2)) demowrite("i xor i2",ixor(i,i2)) demowrite("i shift " || i2,ishift(i,i2)) demowrite("i shift -" || i2,ishift(i,-i2)) return
end
procedure demowrite(vs,v) return write(vs, ": ", v, " = ", int2bit(v),"b") end</lang>
Icon/Unicon implements bitwise operations on integers. Because integers can be transparently large integers operations that require fixed sizes don't make sense and aren't defined. These include rotation and logical shifting (shift is arithmetic) . Please note also that 'not' is a reserved word and the negation function is 'icom'
Sample output:
i: 255 = 11111111b i2: 2 = 10b complement i: -256 = -100000000b i or i2: 255 = 11111111b i and i2: 2 = 10b i xor i2: 253 = 11111101b i shift 2: 1020 = 1111111100b i shift -2: 63 = 111111b i: -15 = -1111b i2: 3 = 11b complement i: 14 = 1110b i or i2: -13 = -1101b i and i2: 1 = 1b i xor i2: -14 = -1110b i shift 3: -120 = -1111000b i shift -3: -2 = -10b
Inform 6
Inform 6 has no xor or rotate operators. It also has no shift operators, although the Z-machine, its usual target architecture, does. These can be accessed with inline assembly, which is done here.
<lang Inform 6>[ bitwise a b temp;
print "a and b: ", a & b, "^"; print "a or b: ", a | b, "^"; print "not a: ", ~a, "^"; @art_shift a b -> temp; print "a << b (arithmetic): ", temp, "^"; temp = -b; @art_shift a temp -> temp; print "a >> b (arithmetic): ", temp, "^"; @log_shift a b -> temp; print "a << b (logical): ", temp, "^"; temp = -b; @log_shift a temp -> temp; print "a >> b (logical): ", temp, "^";
]; </lang>
J
Here are the "bitwise operators":
<lang j>bAND=: 17 b. NB. 16+#.0 0 0 1 bOR=: 23 b. NB. 16+#.0 1 1 1 bXOR=: 22 b. NB. 16+#.0 1 1 0 b1NOT=: 28 b. NB. 16+#.1 1 0 0 bLshift=: 33 b.~ NB. see http://www.jsoftware.com/help/release/bdot.htm bRshift=: 33 b.~ - bRAshift=: 34 b.~ - bLrot=: 32 b.~ bRrot=: 32 b.~ -</lang>
And here is a routine which takes a list of bitwise operators and two numbers and displays a table of results from combining those two numbers with each of the operators:
<lang j>bitwise=: 1 :0
smoutput (((":x),"1' ',.(>u),.' '),"1":y),"1' => ',"1'.X'{~#:x u`:0 y
)</lang>
And here they are in action:
<lang j> 254 bAND`bOR`bXOR`b1NOT`bLshift`bRshift`bRAshift`bLrot`bRrot bitwise 3 254 bAND 3 => ............................X. 254 bOR 3 => ......................XXXXXXXX 254 bXOR 3 => ......................XXXXXX.X 254 b1NOT 3 => XXXXXXXXXXXXXXXXXXXXXX.......X 254 bLshift 3 => ...................XXXXXXX.... 254 bRshift 3 => .........................XXXXX 254 bRAshift 3 => .........................XXXXX 254 bLrot 3 => ...................XXXXXXX.... 254 bRrot 3 => .........................XXXXX</lang>
Further test <lang j> bXOR/ 3333 5555 7777 9999 8664 </lang>
Java
<lang java>public static void bitwise(int a, int b){
System.out.println("a AND b: " + (a & b)); System.out.println("a OR b: "+ (a | b)); System.out.println("a XOR b: "+ (a ^ b)); System.out.println("NOT a: " + ~a); System.out.println("a << b: " + (a << b)); // left shift System.out.println("a >> b: " + (a >> b)); // arithmetic right shift System.out.println("a >>> b: " + (a >>> b)); // logical right shift System.out.println("a rol b: " + Integer.rotateLeft(a, b)); //rotate left, Java 1.5+ System.out.println("a ror b: " + Integer.rotateRight(a, b)); //rotate right, Java 1.5+
}</lang> All of the operators may be combined with the = operator to save space. For example, the following lines each do the same thing: <lang java>a <<= 3; a = a << 3; a *= 8; //2 * 2 * 2 = 8 a = a * 8;</lang>
JavaScript
There are no integers in Javascript, but there are still bitwise operators. They will convert their number operands into integers before performing they task. In other languages, these operators are very close to the hardware and very fast. In JavaScript, they are very far from the hardware and very slow and rarely used.
<lang javascript>function bitwise(a, b){
alert("a AND b: " + (a & b)); alert("a OR b: "+ (a | b)); alert("a XOR b: "+ (a ^ b)); alert("NOT a: " + ~a); alert("a << b: " + (a << b)); // left shift alert("a >> b: " + (a >> b)); // arithmetic right shift alert("a >>> b: " + (a >>> b)); // logical right shift
}</lang>
LFE
All these operations are built-in functions except right arithmetic shift, left rotate, and right rotate. <lang lisp>(defun bitwise (a b)
(io:format '"~p~n" (list (band a b))) (io:format '"~p~n" (list (bor a b))) (io:format '"~p~n" (list (bxor a b))) (io:format '"~p~n" (list (bnot a))) (io:format '"~p~n" (list (bsl a b))) (io:format '"~p~n" (list (bsr a b))))
(defun d2b
(x) (integer_to_list x 2))
(defun bitwise
((a b 'binary) (io:format '"(~s ~s ~s): ~s~n" (list "band" (d2b a) (d2b b) (d2b (band a b)))) (io:format '"(~s ~s ~s): ~s~n" (list "bor" (d2b a) (d2b b) (d2b (bor a b)))) (io:format '"(~s ~s ~s): ~s~n" (list "bxor" (d2b a) (d2b b) (d2b (bxor a b)))) (io:format '"(~s ~s): ~s~n" (list "bnot" (d2b a) (d2b (bnot a)))) (io:format '"(~s ~s ~s): ~s~n" (list "bsl" (d2b a) (d2b b) (d2b (bsl a b)))) (io:format '"(~s ~s ~s): ~s~n" (list "bsr" (d2b a) (d2b b) (d2b (bsr a b))))))
</lang>
Example usage: <lang lisp> > (bitwise 255 170) 170 255 85 -256 381627307539845370001346183518875822092557105621893120 0 ok > (bitwise 255 170 'binary) (band 11111111 10101010): 10101010 (bor 11111111 10101010): 11111111 (bxor 11111111 10101010): 1010101 (bnot 11111111): -100000000 (bsl 11111111 10101010): 1111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (bsr 11111111 10101010): 0 ok > </lang>
Liberty BASIC
Written as functions. <lang lb> ' bitwise operations on byte-sized variables
v =int( 256 *rnd( 1))
s = 1
print "Shift ="; s; " place." print print "Number as dec. = "; v; " & as 8-bits byte = ", dec2Bin$( v) print "NOT as dec. = "; bitInvert( v), dec2Bin$( bitInvert( v)) print "Shifted left as dec. = "; shiftLeft( v, s), dec2Bin$( shiftLeft( v, s)) print "Shifted right as dec. = "; shiftRight( v, s), dec2Bin$( shiftRight( v, s)) print "Rotated left as dec. = "; rotateLeft( v, s), dec2Bin$( rotateLeft( v, s)) print "Rotated right as dec. = "; rotateRight( v, s), dec2Bin$( rotateRight( v, s))
end
function shiftLeft( b, n)
shiftLeft =( b *2^n) and 255
end function
function shiftRight( b, n)
shiftRight =int(b /2^n)
end function
function rotateLeft( b, n)
rotateLeft = (( 2^n *b) mod 256) or ( b >127)
end function
function rotateRight( b, n)
rotateRight = (128*( b and 1)) or int( b /2)
end function
function bitInvert( b)
bitInvert =b xor 255
end function
function dec2Bin$( num) ' Given an integer decimal 0 <--> 255, returns binary equivalent as a string
n =num dec2Bin$ ="" while ( num >0) dec2Bin$ =str$( num mod 2) +dec2Bin$ num =int( num /2) wend dec2Bin$ =right$( "00000000" +dec2Bin$, 8)
end function </lang>
LiveCode
<lang LiveCode>put "and:" && (255 bitand 2) & comma into bitops put " or:" && (255 bitor 2) & comma after bitops put " xor:" && (255 bitxor 2) & comma after bitops put " not:" && (bitnot 255) after bitops put bitops
-- Ouput and: 2, or: 255, xor: 253, not: 4294967040</lang> LiveCode does not provide built-in bit-shift operations.
LLVM
<lang llvm>; ModuleID = 'test.o'
- e means little endian
- p
- { pointer size : pointer abi : preferred alignment for pointers }
- i same for integers
- v is for vectors
- f for floats
- a for aggregate types
- s for stack objects
- n
- {size:size:size...}, best integer sizes
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
- this was compiled with mingw32; thus it must be linked to an ABI compatible c library
target triple = "i386-mingw32"
@.str = private constant [13 x i8] c"a and b: %d\0A\00", align 1 ; <[13 x i8]*> [#uses=1] @.str1 = private constant [12 x i8] c"a or b: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1] @.str2 = private constant [13 x i8] c"a xor b: %d\0A\00", align 1 ; <[13 x i8]*> [#uses=1] @.str3 = private constant [11 x i8] c"not a: %d\0A\00", align 1 ; <[11 x i8]*> [#uses=1] @.str4 = private constant [12 x i8] c"a << n: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1] @.str5 = private constant [12 x i8] c"a >> n: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1] @.str6 = private constant [12 x i8] c"c >> b: %d\0A\00", align 1 ; <[12 x i8]*> [#uses=1]
- A function that will do many bitwise opreations to two integer arguments, %a and %b
define void @bitwise(i32 %a, i32 %b) nounwind {
- entry block
entry:
;Register to store (a & b) %0 = and i32 %b, %a ; <i32> [#uses=1] ;print the results %1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind ; <i32> [#uses=0] ;Register to store (a | b) %2 = or i32 %b, %a ; <i32> [#uses=1] ;print the results %3 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str1, i32 0, i32 0), i32 %2) nounwind ; <i32> [#uses=0] ;Register to store (a ^ b) %4 = xor i32 %b, %a ; <i32> [#uses=1] ;print the results %5 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str2, i32 0, i32 0), i32 %4) nounwind ; <i32> [#uses=0] ;Register to store (~a) %not = xor i32 %a, -1 ; <i32> [#uses=1] ;print the results %6 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([11 x i8]* @.str3, i32 0, i32 0), i32 %not) nounwind ; <i32> [#uses=0] ;Register to store (a << b) %7 = shl i32 %a, %b ; <i32> [#uses=1] ;print the results %8 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str4, i32 0, i32 0), i32 %7) nounwind ; <i32> [#uses=0] ;Register to store (a >> b) (a is signed) %9 = ashr i32 %a, %b ; <i32> [#uses=1] ;print the results %10 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str5, i32 0, i32 0), i32 %9) nounwind ; <i32> [#uses=0] ;Register to store (c >> b), where c is unsiged (eg. logical right shift) %11 = lshr i32 %a, %b ; <i32> [#uses=1] ;print the results %12 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str6, i32 0, i32 0), i32 %11) nounwind ; <i32> [#uses=0] ;terminator instruction ret void
}
- Declare external fuctions
declare i32 @printf(i8* nocapture, ...) nounwind</lang>
Logo
<lang logo>to bitwise :a :b
(print [a and b:] BitAnd :a :b) (print [a or b:] BitOr :a :b) (print [a xor b:] BitXor :a :b) (print [not a:] BitNot :a) ; shifts are to the left if positive, to the right if negative (print [a lshift b:] LShift :a :b) (print [a lshift -b:] LShift :a minus :b) (print [-a ashift -b:] AShift minus :a minus :b)
end bitwise 255 5</lang> The output of this program is: <lang logo>a and b: 5 a or b: 255 a xor b: 250 not a: -256 a lshift b: 8160 a lshift -b: 7 -a ashift -b: -8</lang>
Lua
LuaBitOp implements bitwise functionality for Lua:
<lang lua>local bit = require"bit"
local vb = {
0, 1, -1, 2, -2, 0x12345678, 0x87654321, 0x33333333, 0x77777777, 0x55aa55aa, 0xaa55aa55, 0x7fffffff, 0x80000000, 0xffffffff
}
local function cksum(name, s, r)
local z = 0 for i=1,#s do z = (z + string.byte(s, i)*i) % 2147483629 end if z ~= r then error("bit."..name.." test failed (got "..z..", expected "..r..")", 0) end
end
local function check_unop(name, r)
local f = bit[name] local s = "" if pcall(f) or pcall(f, "z") or pcall(f, true) then error("bit."..name.." fails to detect argument errors", 0) end for _,x in ipairs(vb) do s = s..","..tostring(f(x)) end cksum(name, s, r)
end
local function check_binop(name, r)
local f = bit[name] local s = "" if pcall(f) or pcall(f, "z") or pcall(f, true) then error("bit."..name.." fails to detect argument errors", 0) end for _,x in ipairs(vb) do for _,y in ipairs(vb) do s = s..","..tostring(f(x, y)) end end cksum(name, s, r)
end
local function check_binop_range(name, r, yb, ye)
local f = bit[name] local s = "" if pcall(f) or pcall(f, "z") or pcall(f, true) or pcall(f, 1, true) then error("bit."..name.." fails to detect argument errors", 0) end for _,x in ipairs(vb) do for y=yb,ye do s = s..","..tostring(f(x, y)) end end cksum(name, s, r)
end
local function check_shift(name, r)
check_binop_range(name, r, 0, 31)
end
-- Minimal sanity checks. assert(0x7fffffff == 2147483647, "broken hex literals") assert(0xffffffff == -1 or 0xffffffff == 2^32-1, "broken hex literals") assert(tostring(-1) == "-1", "broken tostring()") assert(tostring(0xffffffff) == "-1" or tostring(0xffffffff) == "4294967295", "broken tostring()")
-- Basic argument processing. assert(bit.tobit(1) == 1) assert(bit.band(1) == 1) assert(bit.bxor(1,2) == 3) assert(bit.bor(1,2,4,8,16,32,64,128) == 255) </lang>
The RiscLua dialect, for RISC OS, has 32-bit integers as the default number type. It provides binary operations & (and), | (or), ^^ (xor), << (logical shift left), >> (logical shift right) and a unary operation ~ (negate).
LSE64
<lang lse64>over : 2 pick 2dup : over over
bitwise : \
" A=" ,t over ,h sp " B=" ,t dup ,h nl \ " A and B=" ,t 2dup & ,h nl \ " A or B=" ,t 2dup | ,h nl \ " A xor B=" ,t over ^ ,h nl \ " not A=" ,t ~ ,h nl
\ a \ 7 bitwise # hex literals</lang>
Mathematica / Wolfram Language
Most functions are built-in or can be made really easily: <lang Mathematica>(*and xor and or*) BitAnd[integer1, integer2] BitXor[integer1, integer2] BitOr[integer1, integer2]
(*logical not*) BitNot[integer1]
(*left and right shift*) BitShiftLeft[integer1] BitShiftRight[integer1]
(*rotate digits left and right*) FromDigits[RotateLeft[IntegerDigits[integer1, 2]], 2] FromDigits[RotateRight[IntegerDigits[integer1, 2]], 2]
(*right arithmetic shift*) FromDigits[Prepend[Most[#], #1], 2] &[IntegerDigits[integer1, 2]]</lang> The function BitShiftLeft, BitShiftRight, RotateRight, RotateLeft all take a second argument, which is the displacement, by default it is set to 1. BitAnd, BitXor and BitOr can handle more than 2 arguments: <lang Mathematica>BitXor[3333, 5555, 7777, 9999]</lang> gives back: <lang Mathematica>8664</lang>
MATLAB / Octave
Newer versions of MATLAB have even more bitwise operations than those demonstrated here. A complete list of bitwise operations for the newest version of MATLAB can be found at MathWorks
<lang MATLAB>function bitwiseOps(a,b)
disp(sprintf('%d and %d = %d', [a b bitand(a,b)])); disp(sprintf('%d or %d = %d', [a b bitor(a,b)])); disp(sprintf('%d xor %d = %d', [a b bitxor(a,b)])); disp(sprintf('%d << %d = %d', [a b bitshift(a,b)])); disp(sprintf('%d >> %d = %d', [a b bitshift(a,-b)]));
end</lang>
Output: <lang MATLAB>>> bitwiseOps(255,2) 255 and 2 = 2 255 or 2 = 255 255 xor 2 = 253 255 << 2 = 1020 255 >> 2 = 63</lang>
Maxima
<lang maxima>load(functs)$
a: 3661$ b: 2541$
logor(a, b); /* 4077 */
logand(a, b); /* 2125 */
logxor(a, b); /* 1952 */
/* NOT(x) is simply -x - 1 -a - 1; /* -3662 */
logor(a, -a - 1); /* -1 */
logand(a, -a - 1); /* 0 */</lang>
MAXScript
<lang maxscript>fn bitwise a b = (
format "a and b: %\n" (bit.and a b) format "a or b: %\n" (bit.or a b) format "a xor b: %\n" (bit.xor a b) format "not a: %\n" (bit.not a) format "Left shift a: %\n" (bit.shift a b) format "Right shift a: %\n" (bit.shift a -b)
)
bitwise 255 170</lang>
MAXScript doesn't have arithmetic shift or rotate operations.
ML/I
ML/I only supports bitwise AND and OR operations. These are available from version CKD onwards.
<lang ML/I>MCSKIP "WITH" NL "" Bitwise operations "" assumes macros on input stream 1, terminal on stream 2 MCSKIP MT,<> MCINS %. MCDEF SL SPACES NL AS <MCSET T1=%A1. MCSET T2=%A2. a and b = %%T1.&%T2.. a or b = %%T1.|%T2.. The other operators are not supported. MCSET S10=0 > MCSKIP SL WITH * MCSET S1=1
- MCSET S10=2
</lang>
Modula-3
<lang modula3>MODULE Bitwise EXPORTS Main;
IMPORT IO, Fmt, Word;
VAR c: Word.T;
PROCEDURE Bitwise(a, b: INTEGER) =
BEGIN IO.Put("a AND b: " & Fmt.Int(Word.And(a, b)) & "\n"); IO.Put("a OR b: " & Fmt.Int(Word.Or(a, b)) & "\n"); IO.Put("a XOR b: " & Fmt.Int(Word.Xor(a, b)) & "\n"); IO.Put("NOT a: " & Fmt.Int(Word.Not(a)) & "\n"); c := a; IO.Put("c LeftShift b: " & Fmt.Unsigned(Word.LeftShift(c, b)) & "\n"); IO.Put("c RightShift b: " & Fmt.Unsigned(Word.RightShift(c, b)) & "\n"); IO.Put("c LeftRotate b: " & Fmt.Unsigned(Word.LeftRotate(c, b)) & "\n"); IO.Put("c RightRotate b: " & Fmt.Unsigned(Word.RightRotate(c, b)) & "\n"); END Bitwise;
BEGIN
Bitwise(255, 5);
END Bitwise.</lang>
Output:
a AND b: 5 a OR b: 255 a XOR b: 250 NOT a: -256 c LeftShift b: 1fe0 c RightShift b: 7 c LeftRotate b: 1fe0 c RightRotate b: f8000007
Nemerle
<lang Nemerle>def i = 255; def j = 2;
WriteLine($"$i and $j is $(i & j)"); WriteLine($"$i or $j is $(i | j)"); WriteLine($"$i xor $j is $(i ^ j)"); WriteLine($"not $i is $(~i)"); WriteLine($"$i lshift $j is $(i << j)"); WriteLine($"$i arshift $j is $(i >> j)"); // When the left operand of the >> operator is of a signed integral type,
// the operator performs an arithmetic shift right
WriteLine($"$(i :> uint) rshift $j is $(c >> j)"); // When the left operand of the >> operator is of an unsigned integral type,
// the operator performs a logical shift right
// there are no rotation operators in Nemerle, but you could define your own w/ a macro if you really wanted it</lang>
Nim
<lang nim>proc bitwise(a, b) =
echo "a and b: " , a and b echo "a or b: ", a or b echo "a xor b: ", a xor b echo "not a: ", not a echo "a << b: ", a shl b echo "a >> b: ", a shr b</lang>
NSIS
All bitwise operations in NSIS are handled by the IntOp instruction. <lang nsis>Function Bitwise Push $0 Push $1 Push $2 StrCpy $0 7 StrCpy $1 2
IntOp $2 $0 & $1 DetailPrint "Bitwise AND: $0 & $1 = $2" IntOp $2 $0 | $1 DetailPrint "Bitwise OR: $0 | $1 = $2" IntOp $2 $0 ^ $1 DetailPrint "Bitwise XOR: $0 ^ $1 = $2" IntOp $2 $0 ~ DetailPrint "Bitwise NOT (negate in NSIS docs): ~$0 = $2" DetailPrint "There are no Arithmetic shifts in NSIS" IntOp $2 $0 >> $1 DetailPrint "Right Shift: $0 >> 1 = $2" IntOp $2 $0 << $1 DetailPrint "Left Shift: $0 << $1 = $2" DetailPrint "There are no Rotates in NSIS"
Pop $2
Pop $1
Pop $0
FunctionEnd</lang>
Objeck
<lang objeck>use IO;
bundle Default {
class Test { function : Main(args : String[]) ~ Nil { BitWise(3, 4); }
function : BitWise(a : Int, b : Int) ~ Nil { Console->GetInstance()->Print("a and b: ")->PrintLine(a and b); Console->GetInstance()->Print("a or b: ")->PrintLine(a or b); Console->GetInstance()->Print("a xor b: ")->PrintLine(a xor b); # shift left & right are supported by the compiler and VM but not # exposed to end-users; those instructions are used for optimizations } }
}</lang>
OCaml
<lang ocaml>let bitwise a b =
Printf.printf "a and b: %d\n" (a land b); Printf.printf "a or b: %d\n" (a lor b); Printf.printf "a xor b: %d\n" (a lxor b); Printf.printf "not a: %d\n" (lnot a); Printf.printf "a lsl b: %d\n" (a lsl b); (* left shift *) Printf.printf "a asr b: %d\n" (a asr b); (* arithmetic right shift *) Printf.printf "a lsr b: %d\n" (a lsr b); (* logical right shift *)
- </lang>
Octave
There's no arithmetic shift nor rotation (and the not can be done through a xor)
<lang octave>function bitops(a, b)
s = sprintf("%s %%s %s = %%s\n", dec2bin(a), dec2bin(b)); printf(s, "or", dec2bin(bitor(a, b))); printf(s, "and", dec2bin(bitand(a, b))); printf(s, "xor", dec2bin(bitxor(a, b))); printf(s, "left shift", dec2bin(bitshift(a, abs(b)))); printf(s, "right shift", dec2bin(bitshift(a, -abs(b)))); printf("simul not %s = %s", dec2bin(a), dec2bin(bitxor(a, 0xffffffff)));
endfunction
bitops(0x1e, 0x3);</lang>
Oforth
There is no built-in for not and rotation
<lang Oforth>: bitwise(a, b)
a b bitAnd println a b bitOr println a b bitXor println a bitLeft(b) println a bitRight(b) println ;</lang>
ooRexx
<lang ooRexx>/* ooRexx ************************************************************* / Bit Operations work as in Rexx (of course)
- Bit operations are performed up to the length of the shorter string.
- The rest of the longer string is copied to the result.
- ooRexx introduces the possibility to specify a padding character
- to be used for expanding the shorter string.
- 10.11.2012 Walter Pachl taken over from REXX and extended for ooRexx
- /
a=21 b=347 Say ' a :'c2b(a) ' 'c2x(a) Say ' b :'c2b(b) c2x(b) Say 'bitand(a,b) :'c2b(bitand(a,b)) c2x(bitand(a,b)) Say 'bitor(a,b) :'c2b(bitor(a,b)) c2x(bitor(a,b)) Say 'bitxor(a,b) :'c2b(bitxor(a,b)) c2x(bitxor(a,b)) p='11111111'B Say 'ooRexx only:' Say 'a~bitor(b,p):'c2b(a~bitor(b,p)) c2x(a~bitor(b,p)) Exit c2b: return x2b(c2x(arg(1)))</lang> Output:
a :0011001000110001 3231 b :001100110011010000110111 333437 bitand(a,b) :001100100011000000110111 323037 bitor(a,b) :001100110011010100110111 333537 bitxor(a,b) :000000010000010100110111 010537 ooRexx only: a~bitor(b,p):001100110011010111111111 3335FF
OpenEdge/Progress
The only bit operators available in OpenEdge are the GET-BITS() and PUT-BITS() functions. These functions can be used to implement all bitwise operators.
PARI/GP
Pari does not support bitwise rotations, which have no obvious meaning with arbitrary-precision integers. See also bitnegimply
for another bitwise operator. For shifts, see also shiftmul
.
<lang parigp>bo(a,b)={
print("And: "bitand(a,b)); print("Or: "bitor(a,b)); print("Not: "bitneg(a)); print("Xor: "bitxor(a,b)); print("Left shift: ",a<<b); print("Right shift: ",a>>b);
}</lang>
Pascal
While Standard Pascal does not have bitwise operations, most Pascal implementations (including Turbo Pascal and Delphi) extend the standard logical operators to also provide bitwise operations: <lang pascal>var
a, b: integer;
begin
a := 10; { binary 1010 } b := 12; { binary 1100 } writeln('a and b = ', a and b); { 8 = 1000 } writeln('a or b = ', a or b); { 14 = 1110 } writeln('a xor b = ', a xor b) { 6 = 0110 }
end.</lang>
Perl
<lang perl>use integer;
sub bitwise($$) {
($a, $b) = @_; print 'a and b: '. ($a & $b) ."\n"; print 'a or b: '. ($a | $b) ."\n"; print 'a xor b: '. ($a ^ $b) ."\n"; print 'not a: '. (~$a) ."\n"; print 'a >> b: ', $a >> $b, "\n"; # logical right shift
use integer; # "use integer" enables bitwise operations to return signed ints print "after use integer:\n"; print 'a << b: ', $a << $b, "\n"; # left shift print 'a >> b: ', $a >> $b, "\n"; # arithmetic right shift
}</lang>
Perl 6
<lang perl6>constant MAXINT = uint.Range.max; constant BITS = MAXINT.base(2).chars;
- define rotate ops for the fun of it
multi sub infix:<⥁>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).list.rotate(b).reverse] } multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.rotate(b)] }
sub int-bits (Int $a, Int $b) {
say ; say_bit "$a", $a; say ; say_bit "2's complement $a", +^$a; say_bit "$a and $b", $a +& $b; say_bit "$a or $b", $a +| $b; say_bit "$a xor $b", $a +^ $b; say_bit "$a unsigned shift right $b", ($a +& MAXINT) +> $b; say_bit "$a signed shift right $b", $a +> $b; say_bit "$a rotate right $b", $a ⥁ $b; say_bit "$a shift left $b", $a +< $b; say_bit "$a rotate left $b", $a ⥀ $b;
}
int-bits(7,2); int-bits(-65432,31);
sub say_bit ($message, $value) {
printf("%30s: %{'0' ~ BITS}b\n", $message, $value +& MAXINT);
}</lang>
- Output:
7: 0000000000000000000000000000000000000000000000000000000000000111 2's complement 7: 1111111111111111111111111111111111111111111111111111111111111000 7 and 2: 0000000000000000000000000000000000000000000000000000000000000010 7 or 2: 0000000000000000000000000000000000000000000000000000000000000111 7 xor 2: 0000000000000000000000000000000000000000000000000000000000000101 7 unsigned shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 7 signed shift right 2: 0000000000000000000000000000000000000000000000000000000000000001 7 rotate right 2: 1100000000000000000000000000000000000000000000000000000000000001 7 shift left 2: 0000000000000000000000000000000000000000000000000000000000011100 7 rotate left 2: 0000000000000000000000000000000000000000000000000000000000011100 -65432: 1111111111111111111111111111111111111111111111110000000001101000 2's complement -65432: 0000000000000000000000000000000000000000000000001111111110010111 -65432 and 31: 0000000000000000000000000000000000000000000000000000000000001000 -65432 or 31: 1111111111111111111111111111111111111111111111110000000001111111 -65432 xor 31: 1111111111111111111111111111111111111111111111110000000001110111 -65432 unsigned shift right 31: 0000000000000000000000000000000111111111111111111111111111111111 -65432 signed shift right 31: 1111111111111111111111111111111111111111111111111111111111111111 -65432 rotate right 31: 1111111111111110000000001101000111111111111111111111111111111111 -65432 shift left 31: 1111111111111111100000000011010000000000000000000000000000000000 -65432 rotate left 31: 1111111111111111100000000011010001111111111111111111111111111111
Phix
Phix has four builtin bitwise operations (and/or/xor/not), all of which have sequence variants. There are no builtin shift or rotate operations, but it would be easy to devise one using / or * powers of 2 [which the compiler often optimises to single machine instructions] and the builtins, see C for an example, or use inline assembly as shown below. <lang Phix>enum SHL, SAR, SHR, ROL, ROR function bitop(atom a, integer b, integer op) atom res
#ilASM{ [32] mov eax,[a] call :%pLoadMint mov ecx,[b] mov edx,[op] cmp dl,SHL jne @f shl eax,cl jmp :storeres @@: cmp dl,SAR jne @f sar eax,cl jmp :storeres @@: cmp dl,SHR jne @f shr eax,cl jmp :storeres @@: cmp dl,ROL jne @f rol eax,cl jmp :storeres @@: cmp dl,ROR jne @f ror eax,cl jmp :storeres @@: int3 ::storeres lea edi,[res] call :%pStoreMint [64] mov rax,[a] mov rcx,[b] mov edx,[op] cmp dl,SHL jne @f shl rax,cl jmp :storeres @@: cmp dl,SAR jne @f sar rax,cl jmp :storeres @@: cmp dl,SHR jne @f shr rax,cl jmp :storeres @@: cmp dl,ROL jne @f rol rax,cl jmp :storeres @@: cmp dl,ROR jne @f ror eax,cl jmp :storeres @@: int3 ::storeres lea rdi,[res] call :%pStoreMint } return res
end function
procedure bitwise(atom a, atom b)
printf(1,"and_bits(%b,%b) = %032b\n",{a,b,and_bits(a,b)}) printf(1," or_bits(%b,%b) = %032b\n",{a,b, or_bits(a,b)}) printf(1,"xor_bits(%b,%b) = %032b\n",{a,b,xor_bits(a,b)}) printf(1,"not_bits(%b) = %032b\n",{a,not_bits(a)}) printf(1," shl(%b,%b) = %032b\n",{a,b,bitop(a,b,SHL)}) printf(1," sar(%b,%b) = %032b\n",{a,b,bitop(a,b,SAR)}) printf(1," shr(%b,%b) = %032b\n",{a,b,bitop(a,b,SHR)}) printf(1," rol(%b,%b) = %032b\n",{a,b,bitop(a,b,ROL)}) printf(1," ror(%b,%b) = %032b\n",{a,b,bitop(a,b,ROR)})
end procedure
bitwise(0x800000FE,7)</lang>
- Output:
and_bits(10000000000000000000000011111110,111) = 00000000000000000000000000000110 or_bits(10000000000000000000000011111110,111) = 10000000000000000000000011111111 xor_bits(10000000000000000000000011111110,111) = 10000000000000000000000011111001 not_bits(10000000000000000000000011111110) = 01111111111111111111111100000001 shl(10000000000000000000000011111110,111) = 00000000000000000111111100000000 sar(10000000000000000000000011111110,111) = 11111111000000000000000000000001 shr(10000000000000000000000011111110,111) = 00000001000000000000000000000001 rol(10000000000000000000000011111110,111) = 00000000000000000111111101000000 ror(10000000000000000000000011111110,111) = 11111101000000000000000000000001
PHP
<lang php>function bitwise($a, $b) {
function zerofill($a,$b) { if($a>=0) return $a>>$b; if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1); // this line shifts a 0 into the sign bit for compatibility, replace with "if($b==0) return $a;" if you need $b=0 to mean that nothing happens return ((~$a)>>$b)^(0x7fffffff>>($b-1));
echo '$a AND $b: ' . $a & $b . '\n'; echo '$a OR $b: ' . $a | $b . '\n'; echo '$a XOR $b: ' . $a ^ $b . '\n'; echo 'NOT $a: ' . ~$a . '\n'; echo '$a << $b: ' . $a << $b . '\n'; // left shift echo '$a >> $b: ' . $a >> $b . '\n'; // arithmetic right shift echo 'zerofill($a, $b): ' . zerofill($a, $b) . '\n'; // logical right shift
}</lang>
PicoLisp
PicoLisp has no specific word size. Numbers grow to arbitrary length. Therefore, bitwise NOT, logical (non-arithmetic) SHIFTs, and rotate operations do not make sense.
Bitwise AND: <lang PicoLisp>: (& 6 3) -> 2
- (& 7 3 1)
-> 1</lang> Bitwise AND-Test (tests if all bits in the first argument are set in the following arguments): <lang PicoLisp>: (bit? 1 2) -> NIL
- (bit? 6 3)
-> NIL
- (bit? 6 15 255)
-> 6</lang> Bitwise OR: <lang PicoLisp>: (| 1 2) -> 3
- (| 1 2 4 8)
-> 15</lang> Bitwise XOR: <lang PicoLisp>: (x| 2 7) -> 5
- (x| 2 7 1)
-> 4</lang> Shift (right with a positive count, left with a negative count): <lang PicoLisp>: (>> 1 8) -> 4
- (>> 3 16)
-> 2
- (>> -3 16)
-> 128
- (>> -1 -16)
-> -32</lang>
PL/I
<lang pli>/* PL/I can perform bit operations on binary integers. */ k = iand(i,j); k = ior(i,j); k = inot(i,j); k = ieor(i,j); k = isll(i,n); /* unsigned shifts i left by n places. */ k = isrl(i,n); /* unsigned shifts i right by n places. */ k = lower2(i, n); /* arithmetic right shift i by n places. */ k = raise2(i, n); /* arithmetic left shift i by n places. */
/* PL/I can also perform boolean operations on bit strings */ /* of any length: */
declare (s, t, u) bit (*);
u = s & t; /* logical and */ u = s | t; /* logical or */ u = ^ s; /* logical not */ u = s ^ t; /* exclusive or */
Built-in rotate functions are not available. They can be readily implemented by the user, though:
u = substr(s, length(s), 1) || substr(s, 1, length(s)-1); /* implements rotate right. */ u = substr(s, 2) || substr(s, 1, 1); /* implements rotate left. */ </lang>
Pop11
<lang pop11>define bitwise(a, b);
printf(a && b, 'a and b = %p\n'); printf(a || b, 'a or b = %p\n'); printf(a ||/& b, 'a xor b = %p\n'); printf(~~ a, 'not a = %p\n'); printf(a << b, 'left shift of a by b = %p\n'); printf(a >> b, 'arithmetic right shift of a by b = %p\n');
enddefine;</lang>
Conceptually in Pop11 integers have infinite precision, in particular negative numbers conceptually have infinitely many leading 1's in two's complement notation. Hence, logical right shift is not defined. If needed, logical right shift can be simulated by masking high order bits.
Similarly, on infinitely precise numbers rotation is undefined.
PureBasic
<lang PureBasic>Procedure Bitwise(a, b)
Debug a & b ; And Debug a | b ;Or Debug a ! b ; XOr Debug ~a ;Not Debug a << b ; shift left Debug a >> b ; arithmetic shift right ; Logical shift right and rotates are not available ; You can of use inline ASM to achieve this: Define Temp ; logical shift right !mov edx, dword [p.v_a] !mov ecx, dword [p.v_b] !shr edx, cl !mov dword [p.v_Temp], edx Debug Temp ; rotate left !mov edx, dword [p.v_a] !mov ecx, dword [p.v_b] !rol edx, cl !mov dword [p.v_Temp], edx Debug Temp ; rotate right !mov edx, dword [p.v_a] !mov ecx, dword [p.v_b] !ror edx, cl !mov dword [p.v_Temp], edx Debug Temp
EndProcedure</lang>
PowerShell
Logical right shift and rotations are not supported in PowerShell.
<lang PowerShell>$X -band $Y $X -bor $Y $X -bxor $Y -bnot $X</lang>
<lang PowerShell>$X -shl $Y
- Arithmetic right shift
$X -shr $Y
- Requires quite a stretch of the imagination to call this "native" support of right rotate, but it works
[System.Security.Cryptography.SHA256Managed].GetMethod('RotateRight', 'NonPublic, Static', $null, @([UInt32], [Int32]), $null).Invoke($null, @([uint32]$X, $Y))</lang>
Python
<lang python>def bitwise(a, b):
print 'a and b:', a & b print 'a or b:', a | b print 'a xor b:', a ^ b print 'not a:', ~a print 'a << b:', a << b # left shift print 'a >> b:', a >> b # arithmetic right shift</lang>
Python does not have built in rotate or logical right shift operations.
Note: Newer Python versions (circa 2.4?) will automatically promote integers into "long integers" (arbitrary length, bounded by available memory). This can be noticed especially when using left shift operations. When using bitwise operations one usually wants to keep these bounded to specific sizes such as 8, 16, 32 or 64 bit widths. To do these we use the AND operator with specific values (bitmasks). For example:
<lang python># 8-bit bounded shift: x = x << n & 0xff
- ditto for 16 bit:
x = x << n & 0xffff
- ... and 32-bit:
x = x << n & 0xffffffff
- ... and 64-bit:
x = x << n & 0xffffffffffffffff</lang>
We can easily implement our own rotation functions. For left rotations this is down by ORing the left shifted and masked lower bits against the right shifted upper bits. For right rotations we perform the converse operations, ORing a set of right shifted lower bits against the appropriate number of left shifted upper bits.
<lang python>def bitstr(n, width=None):
"""return the binary representation of n as a string and optionally zero-fill (pad) it to a given length """ result = list() while n: result.append(str(n%2)) n = int(n/2) if (width is not None) and len(result) < width: result.extend(['0'] * (width - len(result))) result.reverse() return .join(result)
def mask(n):
"""Return a bitmask of length n (suitable for masking against an int to coerce the size to a given length) """ if n >= 0: return 2**n - 1 else: return 0
def rol(n, rotations=1, width=8):
"""Return a given number of bitwise left rotations of an integer n, for a given bit field width. """ rotations %= width if rotations < 1: return n n &= mask(width) ## Should it be an error to truncate here? return ((n << rotations) & mask(width)) | (n >> (width - rotations))
def ror(n, rotations=1, width=8):
"""Return a given number of bitwise right rotations of an integer n, for a given bit field width. """ rotations %= width if rotations < 1: return n n &= mask(width) return (n >> rotations) | ((n << (width - rotations)) & mask(width))</lang>
In this example we show a relatively straightforward function for converting integers into strings of bits, and another simple mask() function to create arbitrary lengths of bits against which we perform our masking operations. Also note that the implementation of these functions defaults to single bit rotations of 8-bit bytes. Additional arguments can be used to over-ride these defaults. Any case where the number of rotations modulo the width is zero represents a rotation of all bits back to their starting positions. This implementation should handle any integer number of rotations over bitfields of any valid (positive integer) length.
R
Native functions in R 3.x
<lang r># Since R 3.0.0, the base package provides bitwise operators, see ?bitwAnd
a <- 35 b <- 42 bitwAnd(a, b) bitwOr(a, b) bitwXor(a, b) bitwNot(a) bitwShiftL(a, 2) bitwShiftR(a, 2)
- See also http://cran.r-project.org/src/base/NEWS.html</lang>
Using as.hexmode or as.octmode
<lang r>a <- as.hexmode(35) b <- as.hexmode(42) as.integer(a & b) # 34 as.integer(a | b) # 43 as.integer(xor(a, b)) # 9</lang>
Using intToBits
The logical operators in R, namely &, | and !, are designed to work on logical vectors rather than bits. It is possible to convert from integer to logical vector and back to make these work as required, e.g. <lang R>intToLogicalBits <- function(intx) as.logical(intToBits(intx)) logicalBitsToInt <- function(lb) as.integer(sum((2^(0:31))[lb])) "%AND%" <- function(x, y) {
logicalBitsToInt(intToLogicalBits(x) & intToLogicalBits(y))
} "%OR%" <- function(x, y) {
logicalBitsToInt(intToLogicalBits(x) | intToLogicalBits(y))
}
35 %AND% 42 # 34 35 %OR% 42 # 42</lang>
Using bitops package
<lang R>library(bitops) bitAnd(35, 42) # 34 bitOr(35, 42) # 43 bitXor(35, 42) # 9 bitFlip(35, bitWidth=8) # 220 bitShiftL(35, 1) # 70 bitShiftR(35, 1) # 17
- Note that no bit rotation is provided in this package</lang>
<lang r># As one can see from getDLLRegisteredRoutines(getLoadedDLLs()$base)
- R knows functions bitwiseAnd, bitwiseOr, bitwiseXor and bitwiseNot.
- Here is how to call them (see ?.Call for the calling mechanism):
.Call("bitwiseOr", as.integer(12), as.integer(10)) .Call("bitwiseXor", as.integer(12), as.integer(10)) .Call("bitwiseAnd", as.integer(12), as.integer(10)) .Call("bitwiseNot", as.integer(12))
- It would be easy to embed these calls in R functions, for better readability
- Also, it's possible to call these functions on integer vectors:
.Call("bitwiseOr", c(5L, 2L), c(3L, 8L))</lang>
Racket
<lang racket>
- lang racket
(define a 255) (define b 5) (list (bitwise-and a b)
(bitwise-ior a b) (bitwise-xor a b) (bitwise-not a) (arithmetic-shift a b) ; left shift (arithmetic-shift a (- b))) ; right shift
</lang> Output:
'(5 255 250 -256 8160 7)
Retro
There is no predefined arithmetic shifts in Retro.
<lang Retro>
- bitwise ( ab- )
cr over "a = %d\n" puts dup "b = %d\n" puts 2over and "a and b = %d\n" puts 2over or "a or b = %d\n" puts 2over xor "a xor b = %d\n" puts over not "not a = %d\n" puts 2over << "a << b = %d\n" puts 2over >> "a >> b = %d\n" puts 2drop ;</lang>
REXX
<lang rexx>/*REXX program performs bitwise operations on integers: & | && ¬ «L »R */ numeric digits 1000 /*be able to handle big integers.*/
say center('decimal',9) center("value",9) center('bits',50) say copies('─',9) copies('─',9) copies('─',50)
a = 21 ; call show a , 'A' /* show & tell A */ b = 3 ; call show b , 'B' /* show & tell B */
call show bAnd(a,b) , 'A & B' /* and */ call show bOr( a,b) , 'A | B' /* or */ call show bXOr(a,b) , 'A && B' /* xor */ call show bNot(a) , '¬ A' /* not */ call show bShiftL(a,b) , 'A [«B]' /* shift left */ call show bShiftR(a,b) , 'A [»B]' /* shirt right */ /*┌───────────────────────────────────────────────────────────────┐ │ Since REXX stores numbers (indeed, all values) as characters, │ │ it makes no sense to "rotate" a value, since there aren't any │ │ boundries for the value. I.E.: there isn't any 32─bit word │ │ "container" or "cell" (for instance) to store an integer. │ └───────────────────────────────────────────────────────────────┘*/
exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────subroutines─────────────────────────*/ show: procedure; parse arg x,t; say right(x,9) center(t,9) right(d2b(x),50); return d2b: return x2b(d2x(arg(1))) +0 /*some REXXes have the D2B bif.*/ b2d: return x2d(b2x(arg(1))) /*some REXXes have the B2D bif.*/ bNot: return b2d(translate(d2b(arg(1)), 10, 01)) +0 /*+0≡normalize.*/ bShiftL: return (b2d(d2b(arg(1)) || copies(0, arg(2)))) +0 bAnd: procedure; parse arg x,y; return c2d(bitand(d2c(x), d2c(y))) bOr: procedure; parse arg x,y; return c2d(bitor( d2c(x), d2c(y))) bXor: procedure; parse arg x,y; return c2d(bitxor(d2c(x), d2c(y)))
bShiftR: procedure; parse arg x,y; $=substr(reverse(d2b(x)), y+1)
if $== then $=0; return b2d(reverse($))</lang>
output
decimal value bits ───────── ───────── ────────────────────────────────────────────────── 21 A 10101 3 B 11 1 A & B 1 23 A | B 10111 22 A && B 10110 10 ¬ A 1010 168 A [«B] 10101000 2 A [»B] 10
RLaB
In RLaB the bitwise operations are available for integers type of numbers. For the operations below if both arguments are integers then the result of the operation is an integer as well.
<lang RLaB>>> x = int(3); >> y = int(1); >> z = x && y; printf("0x%08x\n",z); // logical 'and' 0x00000001 >> z = x || y; printf("0x%08x\n",z); // logical 'or' 0x00000003 >> z = !x; printf("0x%08x\n",z); // logical 'not' 0xfffffffc >> i2 = int(2); >> z = x * i2; printf("0x%08x\n",z); // left-shift is multiplication by 2 where both arguments are integers 0x00000006 >> z = x / i2; printf("0x%08x\n",z); // right-shift is division by 2 where both arguments are integers 0x00000001</lang>
Ruby
<lang ruby>def bitwise(a, b)
form = "%1$7s:%2$6d %2$016b" puts form % ["a", a] puts form % ["b", b] puts form % ["a and b", a & b] puts form % ["a or b ", a | b] puts form % ["a xor b", a ^ b] puts form % ["not a ", ~a] puts form % ["a << b ", a << b] # left shift puts form % ["a >> b ", a >> b] # arithmetic right shift
end
bitwise(14,3)</lang>
- Output:
a: 14 0000000000001110 b: 3 0000000000000011 a and b: 2 0000000000000010 a or b : 15 0000000000001111 a xor b: 13 0000000000001101 not a : -15 ..11111111110001 a << b : 112 0000000001110000 a >> b : 1 0000000000000001
Rust
<lang rust>fn main() {
let a: u8 = 105; let b: u8 = 91; println!("a = {:0>8b}", a); println!("b = {:0>8b}", b); println!("a | b = {:0>8b}", a | b); println!("a & b = {:0>8b}", a & b); println!("a ^ b = {:0>8b}", a ^ b); println!("!a = {:0>8b}", !a); println!("a << 3 = {:0>8b}", a << 3); println!("a >> 3 = {:0>8b}", a >> 3);
}</lang>
Output:
a = 01101001 b = 01011011 a | b = 01111011 a & b = 01001001 a ^ b = 00110010 !a = 10010110 a << 3 = 01001000 a >> 3 = 00001101
SAS
<lang sas>/* rotations are not available, but are easy to implement with the other bitwise operators */ data _null_;
a=105; b=91; c=bxor(a,b); d=band(a,b); e=bor(a,b); f=bnot(a); /* on 32 bits */ g=blshift(a,1); h=brshift(a,1); put _all_;
run;</lang>
Scala
<lang scala>def bitwise(a: Int, b: Int) {
println("a and b: " + (a & b)) println("a or b: " + (a | b)) println("a xor b: " + (a ^ b)) println("not a: " + (~a)) println("a << b: " + (a << b)) // left shift println("a >> b: " + (a >> b)) // arithmetic right shift println("a >>> b: " + (a >>> b)) // unsigned right shift println("a rot b: " + Integer.rotateLeft(a, b)) // Rotate Left println("a rol b: " + Integer.rotateRight(a, b)) // Rotate Right
}</lang>
Scheme
<lang scheme>(import (rnrs arithmetic bitwise (6)))
(define (bitwise a b)
(display (bitwise-and a b)) (newline) (display (bitwise-ior a b)) (newline) (display (bitwise-xor a b)) (newline) (display (bitwise-not a)) (newline) (display (bitwise-arithmetic-shift-right a b)) (newline))
(bitwise 255 5)</lang> Output: <lang>5 255 250 -256 7</lang>
Note: bitwise operations were also described in SRFI-60, with additional aliases (and previously discussed in SRFI-33 which remained draft).
Seed7
The type integer is intended for arithmetic operations. Besides arithmetic shifts, which are seen as multiplication and division by powers of two, no bitwise operations are supported. The type bin32 is intended for bit-pattern operations. Bin32 has the same internal representation as integer. That way conversions between them don't cause an overhead. Right shifting of bin32 values is done with logical shifts.
<lang seed7>$ include "seed7_05.s7i";
include "bin32.s7i";
const proc: bitwise (in integer: a, in integer: b) is func
begin writeln("a: " <& a radix 2 lpad0 32); writeln("b: " <& b radix 2 lpad0 32); writeln("integer operations:"); writeln("a << b: " <& a << b radix 2 lpad0 32); # left shift writeln("a >> b: " <& a >> b radix 2 lpad0 32); # arithmetic right shift end func;
const proc: bitwise (in bin32: a, in bin32: b) is func
begin writeln("bin32 operations:"); writeln("a and b: " <& a & b radix 2 lpad0 32); writeln("a or b: " <& a | b radix 2 lpad0 32); writeln("a xor b: " <& a >< b radix 2 lpad0 32); writeln("not a: " <& ~a radix 2 lpad0 32); writeln("a << b: " <& a << ord(b) radix 2 lpad0 32); # left shift writeln("a >> b: " <& a >> ord(b) radix 2 lpad0 32); # logical right shift writeln("a rotL b: " <& rotLeft(a, ord(b)) radix 2 lpad0 32); # Rotate Left writeln("a rolR b: " <& rotRight(a, ord(b)) radix 2 lpad0 32); # Rotate Right end func;
const proc: main is func
begin bitwise(65076, 6); bitwise(bin32(65076), bin32(6)); end func;</lang>
- Output:
a: 00000000000000001111111000110100 b: 00000000000000000000000000000110 integer operations: a << b: 00000000001111111000110100000000 a >> b: 00000000000000000000001111111000 bin32 operations: a and b: 00000000000000000000000000000100 a or b: 00000000000000001111111000110110 a xor b: 00000000000000001111111000110010 not a: 11111111111111110000000111001011 a << b: 00000000001111111000110100000000 a >> b: 00000000000000000000001111111000 a rotL b: 00000000001111111000110100000000 a rolR b: 11010000000000000000001111111000
Sidef
<lang ruby>func bitwise(a, b) {
# Make sure they are integers a.to_int!; b.to_int!;
say ('a and b : ', a & b); say ('a or b : ', a | b); say ('a xor b : ', a ^ b); say ('not a : ', ~a); say ('a << b : ', a << b); # left shift say ('a >> b : ', a >> b); # arithmetic right shift
}
bitwise(14,3)</lang>
- Output:
a and b : 2 a or b : 15 a xor b : 13 not a : -15 a << b : 112 a >> b : 1
Slate
<lang slate>[ |:a :b |
inform: (a bitAnd: b) printString. inform: (a bitOr: b) printString. inform: (a bitXor: b) printString. inform: (a bitNot) printString. inform: (a << b) printString. inform: (a >> b) printString.
] applyTo: {8. 12}.</lang> Bold text
Smalltalk
Since GNU Smalltalk by default runs without a graphical user interface, I wrote the program in that dialect. The actual methods for bitwise operations (bitAnd:, etc.) are the same in all implementations. <lang smalltalk>| testBitFunc | testBitFunc := [ :a :b |
('%1 and %2 is %3' % { a. b. (a bitAnd: b) }) displayNl. ('%1 or %2 is %3' % { a. b. (a bitOr: b) }) displayNl. ('%1 xor %2 is %3' % { a. b. (a bitXor: b) }) displayNl. ('not %1 is %2' % { a. (a bitInvert) }) displayNl. ('%1 left shift %2 is %3' % { a. b. (a bitShift: b) }) displayNl. ('%1 right shift %2 is %3' % { a. b. (a bitShift: (b negated)) }) displayNl. ].
testBitFunc value: 16r7F value: 4 .</lang>
in addition to the above,
<lang smalltalk>(a bitClear: b) "mask out bits" (a bitAt: index) "retrieve a bit (bit-index, one-based)" (a setBit: index) "set a bit (bit-index)" (a clearBit: index) "clear a bit (bit-index)" (a invertBit: index) "invert a bit (bit index)" lowBit "find the index of the lowest one-bit; zero if none" highBit "find the index of the highest one-bit; zero if none" bitCount "count the one-bits"</lang>
Notice that all of those work on arbitrarily large integers (i.e. 1000 factorial lowBit -> 995).
Standard ML
For integers, IntInfs provide bitwise operations: <lang sml>fun bitwise_ints (a, b) = (
print ("a and b: " ^ IntInf.toString (IntInf.andb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n"); print ("a or b: " ^ IntInf.toString (IntInf.orb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n"); print ("a xor b: " ^ IntInf.toString (IntInf.xorb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n"); print ("not a: " ^ IntInf.toString (IntInf.notb (IntInf.fromInt a )) ^ "\n"); print ("a lsl b: " ^ IntInf.toString (IntInf.<< (IntInf.fromInt a, Word.fromInt b )) ^ "\n"); (* left shift *) print ("a asr b: " ^ IntInf.toString (IntInf.~>> (IntInf.fromInt a, Word.fromInt b )) ^ "\n") (* arithmetic right shift *)
)</lang> More shifts are available for words (unsigned ints): <lang sml>fun bitwise_words (a, b) = (
print ("a and b: " ^ Word.fmt StringCvt.DEC (Word.andb (a, b)) ^ "\n"); print ("a or b: " ^ Word.fmt StringCvt.DEC (Word.orb (a, b)) ^ "\n"); print ("a xor b: " ^ Word.fmt StringCvt.DEC (Word.xorb (a, b)) ^ "\n"); print ("not a: " ^ Word.fmt StringCvt.DEC (Word.notb a ) ^ "\n"); print ("a lsl b: " ^ Word.fmt StringCvt.DEC (Word.<< (a, b) ) ^ "\n"); (* left shift *) print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.~>> (a, b) ) ^ "\n"); (* arithmetic right shift *) print ("a asr b: " ^ Word.fmt StringCvt.DEC (Word.>> (a, b) ) ^ "\n") (* logical right shift *)
)</lang>
Swift
<lang swift>func bitwise(a: Int, b: Int) {
// All bitwise operations (including shifts) // require both operands to be the same type println("a AND b: \(a & b)") println("a OR b: \(a | b)") println("a XOR b: \(a ^ b)") println("NOT a: \(~a)") println("a << b: \(a << b)") // left shift // for right shifts, if the operands are unsigned, Swift performs // a logical shift; if signed, an arithmetic shift. println("a >> b: \(a >> b)") // arithmetic right shift println("a lsr b: \(Int(bitPattern: UInt(bitPattern: a) >> UInt(bitPattern: b)))") // logical right shift
}
bitwise(-15,3)</lang>
- Output:
a AND b: 1 a OR b: -13 a XOR b: -14 NOT a: 14 a << b: -120 a >> b: -2 a lsr b: 2305843009213693950
SystemVerilog
Verilog, being a hardware description language, had pretty comprehensive support for bit twiddling; though rotation is still a slightly manual operation. Just to be different, I decided to use a couple of 53-bit integers: <lang SystemVerilog>program main;
initial begin bit [52:0] a,b,c; a = 53'h123476547890fe; b = 53'h06453bdef23ca6;
c = a & b; $display("%h & %h = %h", a,b,c); c = a | b; $display("%h | %h = %h", a,b,c); c = a ^ b; $display("%h ^ %h = %h", a,b,c); c = ~ a; $display("~%h = %h", a, c);
c = a << 5; $display("%h << 5 = %h", a, c); c = a >> 5; $display("%h >> 5 = %h", a, c);
c = { a[53-23:0], a[52-:23] }; $display("%h rotate-left 23 = %h", a, c); c = { a[1:0], a[52:2] }; $display("%h rotate-right 2 = %h", a, c); end
endprogram</lang>
If we want to do a variable bit rotation, then we need to think in hardware terms, and build a mux structure (this could be a function, but using a module allows it to be parameterized:
<lang SystemVerilog>module rotate(in, out, shift);
parameter BITS = 32; parameter SHIFT_BITS = 5;
input [BITS-1:0] in; output [BITS-1:0] out; input [SHIFT_BITS-1:0] shift;
always_comb foreach (out[i]) out[i] = in[ (i+shift) % BITS ];
endmodule</lang>
of course, one could always write the foreach loop inline.
Tcl
<lang tcl>proc bitwise {a b} {
puts [format "a and b: %#08x" [expr {$a & $b}]] puts [format "a or b: %#08x" [expr {$a | $b}]] puts [format "a xor b: %#08x" [expr {$a ^ $b}]] puts [format "not a: %#08x" [expr {~$a}]] puts [format "a << b: %#08x" [expr {$a << $b}]] puts [format "a >> b: %#08x" [expr {$a >> $b}]]
}</lang> There are no built-in operations for arithmetic right shift or for bit rotation. Indeed, rotation precludes the use of arbitrary-width integers and can only be defined with respect to a particular width. However, we can simulate these operations for 32-bit values (requires Tcl 8.5): <lang tcl>proc bitwiseUnsupported {a b} {
set bits 0xFFFFFFFF # Force interpretation as a 32-bit unsigned value puts [format "a ArithRightShift b: %#08x" [expr {($a & $bits) >> $b}]] puts [format "a RotateRight b: %#08x" [expr { (($a >> $b) & ($bits >> $b)) | (($a << (32-$b)) & ($bits ^ ($bits >> $b))) }]] puts [format "a RotateLeft b: %#08x" [expr { (($a << $b) & $bits & ($bits << $b)) | (($a >> (32-$b)) & ($bits ^ ($bits << $b))) }]]
}</lang>
TI-89 BASIC
While the TI-89 supports arbitrary-size integers, all bitwise arithmetic is performed on the rightmost 32 bits of the integers' two's complement representation.
The right shift operation fills the new leftmost bit with a copy of the old leftmost bit.
<lang ti89b>bitwise(a,b) Prgm
Local show, oldbase Define show(label, x)=Prgm Local r setMode("Base","DEC") string(x) → r setMode("Base","HEX") Disp label & r & " " & string(x) EndPrgm getMode("Base") → oldbase show("", {a, b}) show("And ", a and b) show("Or ", a or b) show("Xor ", a xor b) show("Not ", not a) Pause "[Press ENTER]" show("LSh ", shift(a,b)) show("RSh ", shift(a,–b)) show("LRo ", rotate(a,b)) show("RRo ", rotate(a,–b)) setMode("Base",oldbase)
EndPrgm</lang>
Visual Basic .NET
<lang vbnet>Sub Test(a as Integer, b as Integer)
WriteLine("And " & a And b) WriteLine("Or " & a Or b) WriteLine("Xor " & a Xor b) WriteLine("Not " & Not a) WriteLine("Left Shift " & a << 2) WriteLine("Right Shift " & a >> 2)
End Sub</lang>
Visual Basic doesn't have built-in support for bitwise rotation.
x86 Assembly
It must be linked with the libc and "start" code; lazyly a gcc bitops.o works, being bitops.o produced by nasm -f elf bitops.asm (I've chosen ELF since I am on a GNU/Linux box) <lang asm> extern printf global main
section .text main mov eax, dword [_a] mov ecx, dword [_b] push ecx push eax
and eax, ecx mov ebx, _opand call out_ops
call get_nums or eax, ecx mov ebx, _opor call out_ops
call get_nums xor eax, ecx mov ebx, _opxor call out_ops
call get_nums shr eax, cl mov ebx, _opshr call out_ops
call get_nums shl eax, cl mov ebx, _opshl call out_ops
call get_nums rol eax, cl mov ebx, _oprol call out_ops
call get_nums ror eax, cl mov ebx, _opror call out_ops
call get_nums sal eax, cl mov ebx, _opsal call out_ops
call get_nums sar eax, cl mov ebx, _opsar call out_ops
mov eax, dword [esp+0] not eax push eax not eax push eax push _opnot push _null push _testn call printf add esp, 20
add esp, 8 ret
out_ops push eax push ecx push ebx push dword [_a] push _test call printf add esp, 20 ret
get_nums mov eax, dword [esp+4] mov ecx, dword [esp+8] ret
section .data
_a dd 11 _b dd 3
section .rodata _test db '%08x %s %08x = %08x', 10, 0 _testn db '%08s %s %08x = %08x', 10, 0 _opand db 'and', 0 _opor db 'or ', 0 _opxor db 'xor', 0 _opshl db 'shl', 0 _opshr db 'shr', 0 _opror db 'ror', 0 _oprol db 'rol', 0 _opnot db 'not', 0 _opsal db 'sal', 0 _opsar db 'sar', 0 _null db 0
end</lang>
XPL0
<lang XPL0>Text(0, "A and B = "); HexOut(0, A and B); CrLf(0); \alternate symbol: & Text(0, "A or B = "); HexOut(0, A or B); CrLf(0); \alternate symbol: ! Text(0, "A xor B = "); HexOut(0, A xor B); CrLf(0); \alternate symbol: | Text(0, "not A = "); HexOut(0, not A); CrLf(0); \alternate symbol: ~ Text(0, "A << B = "); HexOut(0, A << B); CrLf(0); Text(0, "A >> B logical = "); HexOut(0, A >> B); CrLf(0); Text(0, "A >> B arithmetic = "); HexOut(0, A ->> B); CrLf(0);
\Rotate operations must be done by calling a function such as: func ROR(A, B); int A, B; return A>>B ! A<<(32-B);
Text(0, "A ror B = "); HexOut(0, ROR(A,B)); CrLf(0);</lang>
The reason the "!" and "|" symbols may seem reversed is that the OR operator was introduced at a time when only uppercase characters were available (such as on the Apple II). The XOR operator was added later.
zkl
No bitwise rotates. Shifts are unsigned. <lang zkl>(7).bitAnd(1) //-->1 (8).bitOr(1) //-->9 (7).bitXor(1) //-->6 (1).bitNot() : "%,x".fmt(_) //-->ff|ff|ff|ff|ff|ff|ff|fe (7).shiftRight(1) //-->3 (7).shiftLeft(1) //-->0xe (-1).toString(16) //-->ffffffffffffffff (-1).shiftRight(1).toString(16) //-->7fffffffffffffff</lang>
- Programming Tasks
- Discrete math
- Basic Data Operations
- 8051 Assembly
- ACL2
- ActionScript
- Ada
- Aikido
- ALGOL 68
- AutoHotkey
- AWK
- Axe
- Babel
- BASIC
- BASIC256
- Batch File
- BBC BASIC
- Beeswax
- C
- C++
- C sharp
- Clojure
- COBOL
- CoffeeScript
- Common Lisp
- D
- Delphi
- DWScript
- E
- ECL
- Elena
- Elixir
- Erlang
- F Sharp
- Factor
- FALSE
- Forth
- Fortran
- Go
- Groovy
- Haskell
- HicEst
- Icon
- Unicon
- Inform 6
- J
- Java
- JavaScript
- LFE
- Liberty BASIC
- LiveCode
- LLVM
- Logo
- Lua
- LSE64
- LSE64 examples needing attention
- Examples needing attention
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Maxima
- MAXScript
- ML/I
- Modula-3
- Nemerle
- Nim
- NSIS
- Objeck
- OCaml
- Oforth
- OoRexx
- OpenEdge/Progress
- PARI/GP
- Pascal
- Perl
- Perl 6
- Phix
- PHP
- PicoLisp
- PL/I
- Pop11
- PureBasic
- PowerShell
- Python
- R
- Racket
- Retro
- REXX
- RLaB
- Ruby
- Rust
- SAS
- Scala
- Scheme
- Seed7
- Sidef
- Slate
- Smalltalk
- Standard ML
- Swift
- SystemVerilog
- Tcl
- TI-89 BASIC
- Visual Basic .NET
- X86 Assembly
- XPL0
- Zkl
- Bc/Omit
- Dc/Omit
- GUISS/Omit
- TPP/Omit
- UNIX Shell/Omit