Perform operations similar to bit domains in C #

Source: Internet
Author: User
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 );}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.