unit bitwise;
interface

function bytebits(b:byte):string;
function bytehex(b:byte):string;
function membits(p:pointer;n:byte):string;
function memhex(p:pointer;n:byte):string;
function memdec(p:pointer;n:byte):string;

function strng(w:word):string;
function desc_powerof(nr:word; base:byte):string;
procedure show(W:word;s:string);

implementation
function strng(w:word):string;
var s:string; begin str(w,s); strng:=s; end;

function bytebits(b:byte):string;
var m:byte; r:string;
begin
   r:='';  m:=$80;
   while(m>0)do begin
      r:=r+char(byte(b and m>0)+48);
      m:=m shr 1;
   end;
   bytebits:=r;
end;

function bytehex(b:byte):string;
var h,l:byte; r:string;
begin
     h:=b div $10;
     l:=b mod $10;
     if h<10 then r:=chr(h+48) else r:=chr(h+55);
     if l<10 then r:=r+chr(l+48) else r:=r+chr(l+55);
     bytehex:=r;
end;

function memhex(p:pointer;n:byte):string;
var r:string;   pc:PChar;
begin
     if(p=nil)then exit;
     r:='';  pc:=p;
     while(n>0)do begin
         r:=bytehex(byte(pc^))+' '+r;
         inc(pc);   dec(n);
     end;
     memhex:=r;
end;
function membits(p:pointer;n:byte):string;
var r:string;   pc:PChar;
begin
     if(p=nil)then exit;
     r:='';  pc:=p;
     while(n>0)do begin
         r:=bytebits(byte(pc^))+' '+r;
         inc(pc);   dec(n);
     end;
     membits:=r;
end;
function memdec(p:pointer;n:byte):string;
var r:string;   pc:PChar;
begin
     if(p=nil)then exit;
     r:='';  pc:=p;
     while(n>0)do begin
         r:=strng (byte(pc^))+' '+r;
         inc(pc);   dec(n);
     end;
     memdec:=r;
end;
function desc_powerof(nr:word; base:byte):string;
var r:string; n,t,p:word;
begin
     n:=1;p:=0; r:='';
     while n<=nr do begin n:=n*base;p:=p+1; end;
     while nr>0 do begin
       while (n>nr)and (p>0) do begin n:=n div base; dec(p); end;
       t:=nr div n;
       dec(nr,t*n);
       if t>0 then begin
          if t>1 then r:=r+strng(t)+'*';
          r:=r+'{'+strng(n)+'|'+strng(p)+'} + ';
        end;
     end;
     desc_powerof:=r;
end;

procedure show(W:word;s:string); var i:integer;
begin     i:=w;
     writeln(i:6,':',w:6,': ',membits(@w,2),': ',memhex(@w,2),s);
end;
end.