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;
  • 相关阅读:
    Java乔晓松oracle的多表查询
    定义一个地址固定的变量
    J2ME的表单元素及其事件
    J2me的异常处理和多线程
    [置顶] eclipse远程部署,静态文件实时同步插件
    newlisp debugger
    启用isqlplus
    ORA27101: shared memory realm does not exist
    windows下安装node.js及less
    em启动/关闭报错解决
  • 原文地址:https://www.cnblogs.com/doorsky/p/1662156.html
Copyright © 2011-2022 走看看