Bitwise operations: Difference between revisions
Content deleted Content added
Updated D code |
|||
Line 320:
=={{header|D}}==
<lang d>
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) ;▼
}
void testBit(int a, int b) {
writefln("Input: a = %d, b = %d", a, b);
writefln("ROT : rot(%8b, %d) = %032b (%4d)", a, b, rot(a,b), rot(a,b));
}
void main() {
int a =
int b =
}</lang>
Output:
<pre>Input:
AND : 11111111 & 00000010 = 00000000000000000000000000000010 ( 2)▼
▲
LSH : 11111111 << 00000010 = 00000000000000000000001111111100 (1020)
RSH : 11111111 >> 00000010 = 00000000000000000000000000111111 ( 63)
NOT : ~ 11111111 = 11111111111111111111111100000000 (-256)
ROT : rot(11111111, 2) = 11000000000000000000000000111111 (-1073741761)</pre>
Compilers are usually able to optimize the code pattern of the rot function to one CPU instruction plus loads.
=={{header|Delphi}}==
|