c语言
typedef union  Stru_AlarmSwitch{
unsigned int m_AlarmSwitchState;
struct Stru_SwitchBitDefine{
unsigned int m_adc    :1;
unsigned int m_dac :1;
unsigned int m_InLow  :1;
unsigned int m_InHigh :1;
unsigned int m_Power  :1;
unsigned int m_Temp1  :1; //温度Low
unsigned int m_Temp2  :1;//温度 high
unsigned int m_Comp1  :1; //组分Low
unsigned int m_Comp2  :1;//组分high
unsigned int m_Can :1;
unsigned int m_Rsv :22;
}m_Bits;
};
dephi 中怎么定义?????

解决方案 »

  1.   

    好象dephi 不支持 位域 定义,怎办?
      

  2.   

    type
      Stru_SwitchBitDefine = LongWord;
      Stru_AlarmSwitch = record
      case Integer of 
          0:m_AlarmSwitchState: LongWord; 
          1:m_Bits: Stru_SwitchBitDefine;
      end; 
    另一个结构在Delphi当中是没有直接的办法表示的,必须使用附加的方法来处理
    procedure Set_m_adc(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $7FFFFFFF) or ((Value and $1) shl 31);
    end;function Get_m_dac(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 31) and $1;
    end;procedure Set_m_dac(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $BFFFFFFF) or ((Value and $1) shl 30);
    end;function Get_m_dac(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 30) and $1;
    end;procedure Set_m_InLow(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $DFFFFFFF) or ((Value and $1) shl 29);
    end;function Get_m_InLow(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 29) and $1;
    end;procedure Set_m_InHigh(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $EFFFFFFF) or ((Value and $1) shl 28);
    end;function Get_m_InHigh(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 28) and $1;
    end;procedure Set_m_Power(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $F7FFFFFF) or ((Value and $1) shl 27);
    end;function Get_m_Power(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 27) and $1;
    end;procedure Set_m_Temp1(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $FBFFFFFF) or ((Value and $1) shl 26);
    end;function Get_m_Temp1(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 26) and $1;
    end;procedure Set_m_Temp2(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $FDFFFFFF) or ((Value and $1) shl 25);
    end;function Get_m_Temp2(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 25) and $1;
    end;procedure Set_m_Comp1(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $FEFFFFFF) or ((Value and $1) shl 24);
    end;function Get_m_Comp1(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 24) and $1;
    end;procedure Set_m_Comp2(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $FF7FFFFF) or ((Value and $1) shl 23);
    end;function Get_m_Comp2(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 23) and $1;
    end;procedure Set_m_Can(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $FFBFFFFF) or ((Value and $1) shl 22);
    end;function Get_m_Can(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := (Rc shr 22) and $1;
    end;procedure Set_m_Rsv(var Rc:  Stru_SwitchBitDefine; const Value: LongWord);
    begin
      Rc := (Rc and $ffc00000) or (Value and $3fffff);
    end;function Get_m_Rsv(const Rc:  Stru_SwitchBitDefine):LongWord;
    begin
      Result := Rc and $3fffff;
    end;对于高版本比如Delphi2007则可以把这些使用属性来完成转换。
     
      

  3.   

    以前  sdzeng 曾经有以下程序,我试了可以运行
    var 
        bit   :   TBits; 
        Data   :   Int64;     Data1   :   Byte; 
        bit1   :   array   [0..7]   of   Byte;     i   :   Integer; 
    begin 
        bit   :=   TBits.Create; 
        bit.Size   :=   64; 
        Data   :=   $f0f0f0f0f0f0f0f0;     for   i:=63   downto   0   do 
            bit.Bits[63-i]   :=   Boolean(   (Data   shr   i)   and   $1);     for   i:=0   to   bit.Size-1   do 
            ShowMessage(IntToStr(Integer(bit.Bits[i]))); 
        bit.Free;     Data1   :=   $f0; 
        for   i:=7   downto   0   do 
            bit1[7-i]   :=     (Data1   shr   i)   and   $1; 
        for   i:=0   to   7   do 
            ShowMessage(IntToStr(bit1[i])); end; 我仿照他的定义type  Un_AlarmSwitch_Record =Record
       case tag : Integer of
     0:  (m_AlarmSwitchState :Integer);
       1:  (m_Bits :TBits);
     // m_Bits;
    end;
     a1:Un_AlarmSwitch_Record;procedure test;
    begin
      a1.m_Bits :=Tbits.Create();
      a1.m_Bits.Size :=32;
      a1.m_AlarmSwitchState :=$0a0a0aff;
      bb:=True;
      TempInt := $0f0f0f0f0;
      TempStr :=  BoolToStr(bb,False);
       for i:=0 to 32 do
       begin
       bb:= Boolean( $1);
      a1.m_Bits.Bits[9]  := bb;  //////////出错????????????
         TempStr :=  BoolToStr(bb,False);
       ListServerInfo.Items.Add(TempStr);
       end;
       a1.m_Bits.Free;end;
      

  4.   

    请问2楼,按照c语言定义,m_adc  应该在bit位置?
    到哪里下Delphi2007??
      

  5.   

    http://altd.codegear.com/download/radstudio2007/CodeGearRADStudio2007_Dec2007.iso