要对每一位赋值。

解决方案 »

  1.   

    var
      T: TBits;
    begin
      T := TBits.Create;
      T.Size := 8;
      T.Bits[0] := true;
      T.Bits[1] := false;
      ...
    end;
      

  2.   

    使用位运算符,or 或 and,不过要先将操作数转换位integer,可使用ord转换。
      

  3.   

    Function SetValue(var aByte:Byte;pos:Byte;boo:Boolean):boolean;
    type
      Bits = (bit0,bit1,bit2,bit3,bit4,bit5,bit6,bit7) ;
      TSetBits = set of Bits ;
      PsetBits =^TSetBits ;
    const
      Const_Bits : array[0..7] of Bits =(bit0,bit1,bit2,bit3,bit4,bit5,bit6,bit7) ;
    var
      P :PsetBits ;
    begin
     Result := (pos>=0) and (pos<=7) ;
     if not Result then Exit ;
     P :=@aByte ;
     if boo then
       Include(P^,Const_Bits[pos])
     else
       Exclude(P^,Const_Bits[pos]);
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      a : Byte ;
    begin
      a :=0;
      SetValue(a,2,True) ;
      caption := inttostr(a);
    end;
      

  4.   

    简单的方法可以用 shl,shr 和 and,or来完成对bit的操作
      

  5.   

    假如要取value的第index位,可用
    result :=(((1 shl index)and value)<>0);或使用TBits类。
      

  6.   

    type
       TBit = 0..1;// 位读
    function GetBit(AValue: Byte; AIndex: 0..7): TBit;
    const
       Bool_Bit: array[False..True] of TBit = (0, 1);
    begin
       result := Bool_Bit[AValue and (1 shl AIndex)];
    end;// 位写
    procedure SetBit(var AValue: Byte; AIndex: 0..7; ABit: TBit);
    begin
       if ABit = 0 then
          AValue := AValue xor (1 shl AIndex)
       else
          AValue := AValue or (1 shl AIndex);
    end;
      

  7.   

    写得太匆忙, 漏了点...type
       TBit = 0..1;// 位读
    function GetBit(AValue: Byte; AIndex: 0..7): TBit;
    const
       Bool_Bit: array[False..True] of TBit = (0, 1);
    begin
       result := Bool_Bit[AValue and (1 shl AIndex) <> 0];
    end;// 位写
    procedure SetBit(var AValue: Byte; AIndex: 0..7; ABit: TBit);
    begin
       if ABit = 0 then
          AValue := AValue xor (1 shl AIndex)
       else
          AValue := AValue or (1 shl AIndex);
    end;
      

  8.   

    不好意思, 又错了!// 位写
    procedure SetBit(var AValue: Byte; AIndex: 0..7; ABit: TBit);
    begin
       if ABit = 0 then
          AValue := AValue and not (1 shl AIndex)
       else
          AValue := AValue or (1 shl AIndex);
    end;
      

  9.   

    // 位取反
    procedure NotBit(var AValue: Byte; AIndex: 0..7);
    begin
       AValue := AValue xor (1 shl AIndex);
    end;