zoukankan      html  css  js  c++  java
  • 移位操作

    在权限设置或者串口设置等方面个人觉得很有用

     //获取每一位的状态  返回值 状态 0,1
      function GetBitSate(dw1:DWORD; Pos:Byte):Byte;
      //设置每一位的状态  返回值 设置后的数据
      function SetBitSate(dw1:DWORD; Pos:Byte; Sate: Byte): DWORD;
      //获取最后8,16,24位的值   返回值 8位的数值
      function GetValue(dw1:DWORD; Pos:Byte):Integer;
      //设置最后8位的值         返回值 设置后8位的数值
      function SetValue(dw1:DWORD; Pos:Byte; nValue:Integer):DWORD;

    //从第0位开始   31,30,29..0;
    function GetBitSate(dw1:DWORD; Pos:Byte):Byte;
    begin
      Result:=((dw1 and (1 shl Pos))shr Pos);
    end;
    
    function SetBitSate(dw1:DWORD; Pos:Byte; Sate: Byte): DWORD;
    var
      dw2: DWORD;
    begin
      dw2 := dw1;
      Result := dw2;
      if GetBitSate(dw1,Pos) = Sate then Exit;
      case Sate of
        0: Result := dw2 and (not (1 shl Pos));
        1: Result := dw2 or (1 shl Pos);
      end;
    end;
    
    function GetValue(dw1:DWORD; Pos:Byte):Integer;
    begin
      //取数的8位,16位,24位的值
      Result := 0;
      if not pos in[8,16,24] then Exit;
      case Pos of
        8: Result:= dw1 and $FF;
        16: Result:= dw1 and $FFFF;
        24: Result:= dw1 and $FFFFFF;
      end;
    end;
    
    
    
    function SetValue(dw1:DWORD; Pos:Byte; nValue:Integer):DWORD;
    begin
      Result := 0;
      if not pos in[8,16,24] then Exit;
      case Pos of
        8: Result:= dw1 and (not $FF) or nValue;
        16: Result:= dw1 and (not $FFFF) or nValue;
        24: Result:= dw1 and (not $FFFFFF) or nValue;
      end;
    end;
    

    procedure TForm1.btn1Click(Sender: TObject);
    var
      a, b: Byte;
      b1, b2: Byte;
    begin
      //取一个字节中的前4位:v := v and $0F; //低4位
      //取一个字节中的后4位:v := v shr $04; //高4位
      a := 245;
      b1 := a and $0F;
    
     // 1111 0101
     // 0000 1111
      b2 := a shr $04;
      ShowMessage(IntToStr(b1));
      ShowMessage(IntToStr(b2));
    
      //将2个byte高低位合并
      b := (b1 and $0F) or (b2 shl $04);
      ShowMessage(IntToStr(b));
    end;
  • 相关阅读:
    C#流(stream)
    高并发图片(缩略图)处理中间层服务架构设计
    WCF技术剖析之十八:消息契约(Message Contract)和基于消息契约的序列化
    我们一起学习WCF 第六篇文件传输
    远程服务器返回了意外相应:(413) Request Entity Too Large。
    三层架构
    传说中的WCF(9):流与文件传输
    分享WCF文件传输---WCFFileTransfer
    分享基于EF+WCF的通用三层架构及解析
    Winform开发框架之框架演化
  • 原文地址:https://www.cnblogs.com/doorsky/p/1662156.html
Copyright © 2011-2022 走看看