ビット構成を表示する

明解C言語 入門編 > 7. 基本型 >

ビット構成を表示する

Delphi


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