ForNext
Only Do What Only You Can Do
055. ビット構成を表示する
VBScript
JScript
Perl
更新日 : 2010.10.18
sub count_bits { my ($x) = @_; $count = 0; while ($x) { $count++ if ($x & 1) ; $x >>= 1; } return $count; } sub int_bits { return &count_bits(~0); } sub print_bits { my ($x) = @_; for ($i = &int_bits - 1; $i >= 0; $i--) { print ((($x >> $i) & 1) ? '1' : '0'); } print "\n"; } $x = 10000; &print_bits($x);
L:\>perl lesson_07_055.pl 00000000000000000010011100010000
PHP
更新日 : 2010.11.03
<?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"; } $x = 10000; print_bits($x); ?>
L:\>php lesson_07_055.php 00000000000000000010011100010000
Python
更新日 : 2010.11.17
import sys def count_bits(x): count = 0 while (x != 0): if (x & 1 != 0): count +=1 x = (x >> 1) & ~(~0 << (sys.getsizeof(x) * 8)); return count def int_bits(): return count_bits(~0) def print_bits(x): for i in range(int_bits() - 1, -1, -1): if ((x >> i) & 1) != 0: sys.stdout.write("1") else: sys.stdout.write("0") print x = 10000 print_bits(x)
N:\>python lesson_07_055.py 0000000000000000000000000000000000000000000000000000000000000000000000000000000000010011100010000
Ruby
更新日 : 2010.11.01
def count_bits(x) count = 0 while (x != 0) count += 1 if (x & 1 != 0) x = (x >> 1) & ~(~0 << (x.size * 8)) end return count end def int_bits return count_bits(~0) end def print_bits(x) (int_bits - 1).downto(0) do |i| print(((x >> i) & 1) != 0 ? '1' : '0') end puts "" end x = 10000 print_bits(x)
L:\>ruby l:\lesson_07_055.rb 000000000000000000010011100010000
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> int count_bits(unsigned x) { int count = 0; while (x) { if (x & 1u) count++; x >>= 1; } return count; } int int_bits(void) { return count_bits(~0u); } void print_bits(unsigned x) { int i; for (i = int_bits() - 1; i >= 0; i--) putchar(((x >> i) & 1u) ? '1' : '0'); putchar('\n'); } int main(int argc, char* argv[]) { unsigned x = 10000; print_bits(x); return 0; }
R:\>lesson055\project1.exe 00000000000000000010011100010000
C++
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
class Lesson055 { public static void main(String[] args) { int x = 10000; print_bits(x); } static void print_bits(int x) { int i; for (i = int_bits() - 1; i >= 0; i--) System.out.print(((x >>> i) & 1) != 0 ? '1' : '0'); System.out.print('\n'); } static int int_bits() { return count_bits(~0); } static int count_bits(int x) { int count = 0; while (x != 0) { if ((x & 1) != 0) count++; x >>>= 1; } return count; } }
L:\>java Lesson055 00000000000000000010011100010000
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var x:Longword; function count_bits(x:Longword):Integer; var count:Integer; begin count := 0; while (x <> 0) do begin if x and 1 <> 0 then inc(count); x := x shr 1; end; result := count; end; function int_bits():Integer; begin result := count_bits(Longword(not 0)); end; procedure print_bits(x:Longword); var i: Integer; begin for i := int_bits() - 1 downto 0 do begin if x shr i and 1 <> 0 then write('1') else write('0'); end; writeln(''); end; begin x := 10000; print_bits(x); end.
S:\>lesson055\project1.exe 00000000000000000010011100010000