DelPhi统计文件比特算法实现

//DELPHI 读取文件的方法(可以读取大于4G的文件)

Private
    FsFile : string;  //文件名
    FiSize : Int64;   //文件大小(字节数)
    FiCount:  Int64;  //bit统计的累计值
Var
    iFile : Integer;
    High, Low : DWORD; //DWORD为32位整数
    FileSize : Int64;  
begin
    iFile := FileOpen(FsFile, fmOpenRead or fmShareDenyNone);
    High:=0;
    Low :=GetFileSize(iFile, @High); //GetFileSize的使用方法看链接                               {https://blog.csdn.net/liangzhao_jay/article/details/72517114}
              {https://blog.csdn.net/gududanxing/article/details/10209055}
    FileSize := H SHR 32 +Low;  
end;

//DELPHI 计算字节的bit位

1)

function GetBitCount(B: Byte): Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := 0 to 7 do
  begin
    Inc(Result, B and 1);
    B := B shr 1;
  end;
end;

2)

function GetBitCount(B: Byte): Integer;
begin
  Result := (B and 1) + (B and 2) div 2 + (B and 4) div 4 + (B and 8) div 8 + (B    and 16) div 16 + (B and 32) div 32 + (B and 64) div 64 + (B and 128) div 128;
end;

3)

bits_arr : array[0..255] of Integer = (0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3,
2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5,
5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2,
3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3,
3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5,
6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,
4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5,
6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8);
    
function GetBitCount(B: Byte): Integer;
begin
    Result := bits_arr[B];  
end;   

//DELPHI 统计文件大小方法

const
    CSize = 1024 * 1024 * 4{4M};
Var
    iFile: Integer;
    buf: PByte;
    I, rst: Integer;
    
while not Terminated do
begin
    rst := FileRead(iFile, buf^, CSize);
     {FileRead:从指定文件(当前文件指针)中读取相应字节的数据,同时调整文件指针位置。其中,       buf用于保存从文件中读取的数据;CSize表示读取的字节数。返回实际读取的字节数。}
    if rst <= 0 then Break; 
    for I := 0 to rst -1 do
    begin
        FiCount := FiCount + GetBitCount(buf[I]);
    end;
end;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值