論理和・論理積・排他的論理和
明解C言語 入門編 > 7. 基本型 >
論理和・論理積・排他的論理和
PHP
<?php function count_bits($x) { $count = 0; while ($x) { if ($x & 1) $count++; $x = ($x >> 1) & ~(~0 << (PHP_INT_SIZE * 8) - 1); } return $count; } function int_bits() { return count_bits(~0); return count_bits(~0); } function print_bits($x) { for ($i = int_bits() - 1; $i >= 0; $i--) { print ((($x >> $i) & 1) ? '1' : '0'); } print "\n"; } $na = 12345; $nb = 54321; print "A = "; print_bits($na); print "NOT A = "; print_bits(~$na); print "\n"; print "A = "; print_bits($na); print "B = "; print_bits($nb); print "A AND B = "; print_bits($na & $nb); print "\n"; print "A = "; print_bits($na); print "B = "; print_bits($nb); print "A OR B = "; print_bits($na | $nb); print "\n"; print "A = "; print_bits($na); print "B = "; print_bits($nb); print "A XOR B = "; print_bits($na ^ $nb); ?>
実行結果
L:\>php lesson_07_056.php
A = 00000000000000000011000000111001
NOT A = 11111111111111111100111111000110A = 00000000000000000011000000111001
B = 00000000000000001101010000110001
A AND B = 00000000000000000001000000110001A = 00000000000000000011000000111001
B = 00000000000000001101010000110001
A OR B = 00000000000000001111010000111001A = 00000000000000000011000000111001
B = 00000000000000001101010000110001
A XOR B = 00000000000000001110010000001000