//一种获取无符号整数最大值的方法:
procedure TForm1.Button1Click(Sender: TObject);
var
  {Delphi 标准的无符号整数类型只有三种}
  num_Byte     : Byte;
  num_Word     : Word;
  num_Cardinal : Cardinal;

  {Windows API 中相应的类型就太多了, 下面是随便找了几种}
  api_UCHAR    : UCHAR;    {Byte}
  api_LANGID   : LANGID;   {Word}
  api_DWORD    : DWORD;    {Cardinal}
  api_LongWord : LongWord; {Cardinal}
  api_UINT     : UINT;     {Cardinal}
  api_THandle  : THandle;  {Cardinal}
  api_HWND     : HWND;     {Cardinal}
begin
  num_Byte     := Byte(-1);     ShowMessage(IntToStr(num_Byte));     {255}
  num_Word     := Word(-1);     ShowMessage(IntToStr(num_Word));     {65535}
  num_Cardinal := Cardinal(-1); ShowMessage(IntToStr(num_Cardinal)); {4294967295}

  api_UCHAR    := UCHAR(-1);    ShowMessage(IntToStr(api_UCHAR));    {255}
  api_LANGID   := LANGID(-1);   ShowMessage(IntToStr(api_LANGID));   {65535}
  api_DWORD    := DWORD(-1);    ShowMessage(IntToStr(api_DWORD));    {4294967295}
  api_LongWord := LongWord(-1); ShowMessage(IntToStr(api_LongWord)); {4294967295}
  api_UINT     := UINT(-1);     ShowMessage(IntToStr(api_UINT));     {4294967295}
  api_THandle  := THandle(-1);  ShowMessage(IntToStr(api_THandle));  {4294967295}
  api_HWND     := HWND(-1);     ShowMessage(IntToStr(api_HWND));     {4294967295}
end;

简单分析: 譬如 Word 是两字节 16 位的, 它最大值的二进制表示是: 11111111 11111111; 假如它能表示 -1, 负数的算法是: 反码 + 1: 1 的二进制是: 00000000 00000001; 1 的反码是 : 11111111 11111110 补 1 后是 : 11111111 11111111 Word 没有负数, 当然 11111111 11111111 就是它的最大值了.
posted on 2008-02-16 11:36  万一  阅读(5984)  评论(0编辑  收藏  举报