Delphi INT 存储多个数字

//存多个数字 例子
procedure
TForm1.btn1Click(Sender: TObject); var i:integer; A,B,C,d:INTEGER; begin //设计的时候根据使用场景 要存多个数字,每个数字只能占多少 //4个数字 每个数字8bit =1个字节 A := $FF; //也可写成255 B := 10; C := 20; D := 11; //左移 i := 0; i := 0+A; i := i shl 8; i := i+B; i := i shl 8; i := i+C; i := i shl 8; i := i+D; //右移 c := i shr 8; b := c shr 8; a := b shr 8; //最后一个数字 就是最低位 mmo1.Lines.Add('D是'+IntToStr(i and $FF)); //等于前面数字右移后的差 mmo1.Lines.Add('C是'+IntToStr(c - b shl 8)); mmo1.Lines.Add('B是'+IntToStr(b - a shl 8)); //第一个不用计算差值 mmo1.Lines.Add('A是'+IntToStr(A)); end;

第二种写法:

procedure TForm1.btn3Click(Sender: TObject);
var
 i:integer;
 A,B,C,d:INTEGER;
begin
//每个数字最高FF
 A := $AB;
 B := $BC;
 C := $CD;
 D := $DE;

 //左移
 i := 0;
 i := 0+A;
 i := i shl 8;
 i := i+B;
 i := i shl 8;
 i := i+C;
 i := i shl 8;
 i := i+D;


 mmo1.Lines.Add('D是'+IntToHex(i and $FF,2));
 mmo1.Lines.Add('C是'+IntToHex(i shr 8 and  $FF,2));
 mmo1.Lines.Add('B是'+IntToHex(i shr 16 and  $FF,2));
 mmo1.Lines.Add('A是'+IntToHex(i shr 24 and  $FF,2));
end;

 

 

 //存标志位 http://www.138soft.com/?p=209

procedure TForm1.btn2Click(Sender: TObject);
const
 ParamA = $0001;  //二进制00000001
 ParamB = $0002;  //二进制00000010
 ParamC = $0004;  //二进制00000100

var
 Params: Cardinal; //类型具体看场景,可以用word integer  等 要注意 类型是否有符号位 有符号位的话需要考虑解码后需要 原码转补码 补码转原码等情况
begin
 //存 WIN32API传参数也是这样
 Params := ParamA or ParamB or ParamC;
 //判断参数是否有
 if Params and ParamA = ParamA then //存在A
 if Params and ParamB = ParamB then //存在B
 if Params and ParamC = ParamC then //存在B
end;

 

posted @ 2022-06-18 10:43  Tag  阅读(77)  评论(0编辑  收藏  举报