Class Cbitbuffer based on bitwise manipulation

Source: Internet
Author: User
Tags bitwise bool class definition constructor numeric value range reset

Due to the needs of the work, I wrote a bit-based operation of the class, by the CFile class imitation. Write more hasty, many operation efficiency is low, need to improve.

This code is more useful to me, if you have any improvement on the code I hope you can tell me. Email:mymong@163.net

I. Class definitionClass Cbitbuffer
{
Private
Longlong m_lllength; The size of the cache, in units (bits).
BYTE * M_pbegin; The cached start pointer, byte pointer, to the first byte of the cache.
BYTE * m_pend; The end of the cache pointer, the byte pointer, to the next byte of the last byte of the cache.
BYTE * M_pcurbyte; The current pointer to the cache, the byte pointer, pointing to the current byte.
BYTE M_ncuroffset; The bit offset from the beginning of the current byte, with a value range of 0-7.
BOOL m_blocked; If the cache is locked, when you create a cache, the cache is locked before it is released, and the header pointer and cache size cannot be assigned again.
Note: The M_pcurbyte and m_ncuroffset combine to form a bit pointer that can point to the current bit.
Public
"Status related":
BOOL islocked (); is currently a cache-locked state.
BOOL isbytealigned (); Whether the current bit pointer is byte-aligned.
"Input and Output":
Writes the current bit to ' 0 ', and the bit pointer automatically moves back one bit.
void WriteBit0 ();
Writes the current bit to ' 1 ', and the bit pointer automatically moves back one bit.
void WriteBit1 ();
Reads the ncount single-digit from the current bit pointer and converts it to an integer output, and the bit pointer automatically moves back.
(in/out) Ncount: The number of bits read.
Note: This range of values is 0-32 and ensures that reading values are not crossed when you use this function.
Considering the low efficiency, the function body does not add any validation, requiring the user to be careful.
int Read (BYTE ncount);
Writes a string of binary numbers starting from the current bit pointer, which is entered as a literal, and the bit pointer is automatically moved back.
parameter example: "0011 1011 0111 1111", the space is ignored.
"*000 1111 1111 *111", the bit of the asterisk is skipped and not written.
void Writebinary (char * pbinstr);
Writes a 16 number from the current byte pointer, which is entered as a string, the bit pointer automatically moves back, and the bit offset is 0.
parameter example: "BA", the space will be ignored, the string cannot have "0X" characters, and the letters are uppercase.
Note: Before using this function, you must ensure that the byte is aligned, no "0X", uppercase letters, and so on, the function body does not check!
void Writehex (char * phexstr);
Writes from the current bit pointer to the ncount single digit, which corresponds to a specified numeric value that can be entered in decimal or hexadecimal ncount.
void Writedecimal (UINT ndata, int ncount);
Copies a piece of data to the current cache, writes from the current byte pointer, and the bit pointer automatically moves back, and the bit offset is 0.
(in) Psubbuff: The head pointer of the source data.
(in/out) Ncount: The length of the copy, note the number of bytes.
actually writed byte count. The count May is less than ncount if the
End of buffer is reached.
void WriteBuffer (BYTE * psubbuff, int &ncount);
"Location related":
Gets the starting pointer for the cache.
BYTE * Getbegin ();
Gets the end pointer of the cache that points to the next byte of the cached last byte.
BYTE * Getend ();
Gets the current pointer.
BYTE * Getcurbyte ();
Gets the bit offset of the current bit pointer at the current byte.
BYTE Getcuroffset ();
Set the size of the cache, the unit is "bit", but you must ensure a multiple of 8.
Before setting the cache size, you need to make sure that the cache start pointer is set.
Returns the cache size that was last set, and returns 0 if it is set for the first time.
If the cache is locked, the size cannot be reset, and a return of 1 indicates failure.
(in) Lllen: Cache size, Unit is "bit", and is a multiple of 8, preferably entered as a 1024*8.
Longlong SetLength (Longlong lllen);
To get the size of the cache, the unit is "bit" and should be a multiple of 8.
Longlong GetLength ();
Moves the current bit pointer to the end of the cache, and actually points to the first bit of the next byte that caches the last byte.
Returns the size of the cache, which is "bit" and should be a multiple of 8.
Longlong seektoend ();
Moves the current bit pointer to the beginning of the cache, and actually points to the first bit of the first byte of the cache.
void Seektobegin ();
Moves the current bit pointer at a specified value offset, when the offset is positive, moves to the end, and the offset is a negative number when it moves to the start, and the offset unit is "bit."
Returns the offset of the current bit pointer relative to the starting address, in units of "bit" and the return value if it is a negative number indicating failure.
(in) Lloff: offset, unit "bit".
Longlong Seek (Longlong Lloff);
"Constructor":
The default constructor does not set the cache and its size, and so on, notice to set the relevant content before use.
Cbitbuffer ();
The constructor sets the cache and its size. The cache size defaults to 0.
The cache size defaults to 0, and you must set the correct cache size before you actually use it.
Note: It is best not to use this constructor, it is recommended to use the Sharebuffer () function.
Cbitbuffer (BYTE * pbegin, Longlong lllen = 0);
This function resets the starting address of the cache and the cache size, and calculates the ending address.
If you do not set the size of the cache, the capital is set to 0, the correct number must be set before use.
If you already have a data area that you want to manage using Cbitbuffer, it is recommended that you use this function for initialization.
Attention:
If the memory is allocated elsewhere and is managed using Cbitbuffer, the release of that memory is not a cbitbuffer responsibility.
If the Cbitbuffer object has already created a cache using the Create () function, you cannot use the function to reset the cache until the release () function is invoked.
If the Cbitbuffer object already has a cache and is also set with that function, you can use the function to set the new cache again.
The cache set with this function is not locked, so the cache and size can be reset.
When this function is used, false indicates failure if the cache is locked.
(in) Pbegin: The first address of the managed data area, which refers to the address of the first byte.
(in) Lllen: The size of the data area is managed, the unit "bit" must be a multiple of 8, it is recommended to use the form of 1024*8 input.
BOOL Sharebuffer (BYTE * pbegin, Longlong lllen = 0);
Use the size of the input to create a cache, the cache size unit is "bit", must be a multiple of 8, it is recommended to use the form of 1024*8 input.
The cache creation successfully returns TRUE, and the failure returns false.
Note: When you create a cache using this function, the cache is locked, and if the cache is locked, the creation fails.
If you are sure that the cache is no longer needed, remember to call the release () function to unlock and free the cache.
BOOL Create (Longlong lllen);
Unlock the cache and release the cache created by the Create () function.
void release ();
Virtual ~cbitbuffer ();
};

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.