Bitwise operations: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: added comment about missing rotations, required by task description)
m (→‎{{header|D}}: remove incorrect; added shifts (not tested, hard time compiling gdc))
Line 231: Line 231:


=={{header|D}}==
=={{header|D}}==
{{incorrect|D}}
<lang d>module bitwise ;
<lang d>module bitwise ;
import std.stdio ;
import std.stdio ;
Line 240: Line 239:
writefln(" OR : %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("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("NOT : %8s ~ %08b = %032b (%4d)", "", a, ~a, ~a) ;
}
}
// shift and rotation are not available

void main() {
void main() {
int a = 0b11111111 ; // bit literal 255
int a = 0b11111111 ; // bit literal 255
int b = 0b10101010 ; // bit literal 170
int b = 0b00000010 ; // bit literal 2
testbit(a,b) ;
testbit(a,b) ;
}</lang>Output:
}</lang>Output:
<pre>Input: a = 255 , b = 170
<pre>Input: a = 255 , b = 2
AND : 11111111 & 10101010 = 00000000000000000000000010101010 ( 170)
AND : 11111111 & 00000010 = 00000000000000000000000000000010 ( 2)
OR : 11111111 | 10101010 = 00000000000000000000000011111111 ( 255)
OR : 11111111 | 00000010 = 00000000000000000000000011111111 ( 255)
XOR : 11111111 ^ 10101010 = 00000000000000000000000001010101 ( 85)
XOR : 11111111 ^ 00000010 = 00000000000000000000000011111101 ( 253)
LSH : 11111111 << 00000010 = 00000000000000000000001111111100 (1020)
RSH : 11111111 >> 00000010 = 00000000000000000000000000111111 ( 63)
NOT : ~ 11111111 = 11111111111111111111111100000000 (-256)</pre>
NOT : ~ 11111111 = 11111111111111111111111100000000 (-256)</pre>