Perform operations similar to bit domains in C #
Http://www.cnblogs.com/TianFang/archive/2011/01/23/1942446.html
This article is similar to the previous article that implements bigendian numbers in C #. It is all encountered when parsing network protocols and files. Sometimes the Protocol stipulates that a byte number is divided into several parts, and a part represents a number. In this case, the bitwise domain can be easily implemented in C, but it is not that easy in C. Here is a Class I wrote, specifically used to assist in this operation.
/// <Summary>
/// This class is mainly used to divide a byte into several parts, each representing a number type
/// </Summary>
Class bithelper
{
Public static byte getbyte (byte data, byte index, byte count)
{
Contract. Requires (index + count <= 8 );
Return (byte) (Data >>( 8-index-count) % (1 <count ));
}
Public static byte setbyte (INT value, byte data, byte index, byte count)
{
Contract. Requires (index + count <= 8 );
Contract. Requires (value <(1 <count ));
VaR tail = data % (1 <(8-index-count ));
Return (byte) (data> (8-index) <count) + value) <index) + tail );
}
// Test whether a certain digit is 1
Public static bool testbit (byte data, byte index)
{
Return getbyte (data, index, 1) = 1;
}
Public static byte setbit (bool value, byte data, byte index)
{
Return setbyte (value? 1: 0, Data, index, 1 );
}
}
This class cannot be used directly. It is also an auxiliary operation. It is generally used to encapsulate a field in the attribute.
Private byte flags = 5;
Public byte flags
{
Get {return flags;
}
Set {flags = value ;}
}
Public bool typeflagsaudio
{
Get {return bithelper. testbit (flags, 5 );}
Set {flags = bithelper. setbit (value, flags, 5 );}
}
Public bool typeflagsvideo
{
Get {return bithelper. testbit (flags, 7 );}
Set {flags = bithelper. setbit (value, flags, 7 );}
}
This article is similar to the previous article that implements bigendian numbers in C #. It is all encountered when parsing network protocols and files. Sometimes the Protocol stipulates that a byte number is divided into several parts, and a part represents a number. In this case, the bitwise domain can be easily implemented in C, but it is not that easy in C. Here is a Class I wrote, specifically used to assist in this operation.
/// <Summary>
/// This class is mainly used to divide a byte into several parts, each representing a number type
/// </Summary>
Class bithelper
{
Public static byte getbyte (byte data, byte index, byte count)
{
Contract. Requires (index + count <= 8 );
Return (byte) (Data >>( 8-index-count) % (1 <count ));
}
Public static byte setbyte (INT value, byte data, byte index, byte count)
{
Contract. Requires (index + count <= 8 );
Contract. Requires (value <(1 <count ));
VaR tail = data % (1 <(8-index-count ));
Return (byte) (data> (8-index) <count) + value) <index) + tail );
}
// Test whether a certain digit is 1
Public static bool testbit (byte data, byte index)
{
Return getbyte (data, index, 1) = 1;
}
Public static byte setbit (bool value, byte data, byte index)
{
Return setbyte (value? 1: 0, Data, index, 1 );
}
}
This class cannot be used directly. It is also an auxiliary operation. It is generally used to encapsulate a field in the attribute.
Private byte flags = 5;
Public byte flags
{
Get {return flags;
}
Set {flags = value ;}
}
Public bool typeflagsaudio
{
Get {return bithelper. testbit (flags, 5 );}
Set {flags = bithelper. setbit (value, flags, 5 );}
}
Public bool typeflagsvideo
{
Get {return bithelper. testbit (flags, 7 );}
Set {flags = bithelper. setbit (value, flags, 7 );}
}