## 《Delphi 算法与数据结构》学习与感悟[3]: 获取一个字节中非空位的个数 转

涂孟超

``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

{方法1: 获取函数}
function GetByteBits(x: Byte): Byte;
begin
Result := 0;
while x <> 0 do
begin
if Odd(x) then Inc(Result);
x := x shr 1;
end;
end;

{方法2: 把所有可能的值放在一个常数数组}
const
BitArr: array[0..MAXBYTE] of Byte = (
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);

{测试}
procedure TForm1.Button1Click(Sender: TObject);
var
b,num: Byte;
begin
b := 255;
num := GetByteBits(b);      {使用函数获取}
ShowMessage(IntToStr(num)); {8}
num := BitArr[b];           {直接使用数组获取}
ShowMessage(IntToStr(num)); {8}

b := 254;
num := GetByteBits(b);      {使用函数获取}
ShowMessage(IntToStr(num)); {7}
num := BitArr[b];           {直接使用数组获取}
ShowMessage(IntToStr(num)); {7}
end;

end.

``````

``````function GetIntBits(x: Integer): Byte;
begin
Result := 0;
while x <> 0 do
begin
if Odd(x) then Inc(Result);
x := x shr 1;
end;
end;

``````

### 涂孟超

Netty那点事（三）Channel与Pipeline

Channel是理解和使用Netty的核心。Channel的涉及内容较多，这里我使用由浅入深的介绍方法。在这篇文章中，我们主要介绍Channel部分中Pipeline实现机制。为了避免枯燥，借用一下《盗梦空间》的...

2013/11/24
2W
22
【opencv】图形的绘制

1.矩形图像的绘制： 原函数：void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8,int shift=0) img就是需要绘制的图像 pt1 and pt......

2014/10/08
1.2K
1
beego API开发以及自动化文档

beego API开发以及自动化文档 beego1.3版本已经在上个星期发布了，但是还是有很多人不了解如何来进行开发，也是在一步一步的测试中开发，期间QQ群里面很多人都问我如何开发，我的业余时间实在...

astaxie
2014/06/25
2.7W
22
Nutch学习笔记4-Nutch 1.7 的 索引篇 ElasticSearch

2014/06/26
712
0

2013/11/09
7.3K
8

1.关于国旗尺寸与标准的规定 五星红旗的形状为长方形，红色象征革命，其长与高之比为3:2，旗面左上方缀黄色五角星五颗，象征共产党领导下的革命大团结，星用黄色象征红色大地上呈现光明。一星...

pythonexpert
2019/09/30
0
0

07/04
0
0

osc_4dgu16li
6分钟前
0
0

osc_m8miia9c
8分钟前
0
0

04/23
0
0