以下是31位二進位制形式輸出的pascal程式:
{輸入為x,轉換結果為y,y的長度最多31位}
{轉換的範圍為0~2147483647}
const
q:array[1..31] of longint=($1,$2,$4,$8,$10,$20,$40,$80,
$100,$200,$400,$800,$1000,$2000,$4000,$8000,
$10000,$20000,$40000,$80000,
$100000,$200000,$400000,$800000,
$1000000,$2000000,$4000000,$8000000,
$10000000,$20000000,$40000000);
var
i,k:integer;
b:array[1..31] of boolean;
x:longint;
y:string;
begin
x:=2147483647;
for i:=31 downto 1 do begin
if x>=q[i] then begin b[i]:=true;x:=x-q[i]; end
else begin b[i]:=false; end;
end;
k:=0;
for i:=31 downto 1 do
if b[i] then begin k:=i; break; end;
if k=0 then y:="0"
else begin
y:="";
for i:=k downto 1 do
if b[i] then y:=y+"1" else y:=y+"0";
writeln(y);
end.
以下是31位二進位制形式輸出的pascal程式:
{輸入為x,轉換結果為y,y的長度最多31位}
{轉換的範圍為0~2147483647}
const
q:array[1..31] of longint=($1,$2,$4,$8,$10,$20,$40,$80,
$100,$200,$400,$800,$1000,$2000,$4000,$8000,
$10000,$20000,$40000,$80000,
$100000,$200000,$400000,$800000,
$1000000,$2000000,$4000000,$8000000,
$10000000,$20000000,$40000000);
var
i,k:integer;
b:array[1..31] of boolean;
x:longint;
y:string;
begin
x:=2147483647;
for i:=31 downto 1 do begin
if x>=q[i] then begin b[i]:=true;x:=x-q[i]; end
else begin b[i]:=false; end;
end;
k:=0;
for i:=31 downto 1 do
if b[i] then begin k:=i; break; end;
if k=0 then y:="0"
else begin
y:="";
for i:=k downto 1 do
if b[i] then y:=y+"1" else y:=y+"0";
end;
writeln(y);
end.