Tuesday 15 June 2010

bit manipulation - Concatenating Bits in C++ -


I am developing a software for conversion encoder and bit painting.

Then a 55-bit loop data in my program is generated in each loop. It is stored in the first 55 bit of unsigned long time type of variable. After each recurrence, I have to transfer these 55 bits to the unsigned four buffers (buffer is about very close to 500 bytes). In this buffer the bits should be continuously stored (i.e. no difference). Is there some bit trick to add this way easily?

Edit: It was told correctly for me, because I have four-buffer I am aliasing through the uint64_t * , I am violating the strict aliasing rule, and due to the differences in Endianness, the bug is also weak. However, if you have some guarantees about your platform, then it still works. This can be solved by buffer of 64-bit elements, instead of a single bytes.

If you want a buggy algorithm which does not depend on any external libraries, you can use the snippet below to note that the use of bitset only to display the result It is done and ensures that it works.

As a test, I have defined a 55-bit pattern in which 54 consist of 1, followed by a 0. remaining 9 bits 64-bit value ( x ) are also zero. Buffer is a 500-byte alphabet, which I placed in uint64_t * . The algorithm keeps track of the current 64-bit block ( currentBlock ) and the current bit of this block ( currentBit ). It does the following:

  1. Turn on Bitaptation as it starts with the current bit-position
  2. or the result with the current 64-bit block means The first part of the bit-pattern that has been dropped from the existing block has been added. For example, in the second step when the first 55 bits of the first block are filled, the remaining 9 bits will be captured.
  3. Update the currentBit variable
  4. Check if the currentBlock was overflowed, if so, then go to the next block and add the remainder of the 55-bit partition.

      #include & lt; Iostream & gt; # Include & lt; Bitset & gt; // is used for testing only, not algorithms using the namespace STD; Int main () {size_t nBits = 55; // 54 1's, followed by 10 UIT 64_T X = 0B1111111111111111111111111111111111111111111111111111110000000000; // 500 bytes buffer: four buff 8 [500] {}; Uint64_t * buf = reinterpret_cast & lt; Uint64_t * & gt; (And buf8 [0]); // It would be better; Use it if possible / uint64_t buf [100]; Int currentBit = 0; Int currentblock = 0; // 10-fold plus for 55-bit text (int i = 0; i! = 10; ++ i) {buf [currentBlock] | = (X> gt; & gt; current beat); CurrentBit + = NBIT; If (currentBit> = 64) {++ currentBlock; CurrentBit% = 64; Buff [current block] | = X & LT; & Lt; (NBIT - Current Beat); }} // for the examination (int i = 0; i! = 5; ++ i) COAT & lt; & Lt; Bitset & lt; 64 & gt; (Buff [i]) & lt; & Lt; '\ N'; }  

You should probably make it normal and wrap it in a function. It is up to you. The program is, as follows: Production of low production.

  1111111111111111111111111111111111111111111111111111110111111111 1111111111111111111111111111111111111111111110111111111111111111 1111111111111111111111111111111111110111111111111111111111111111 1111111111111111111111111110111111111111111111111111111111111111 1111111111111111110111111111111111111111111111111111111111111111  

Note 0 every 55'th bit marking


No comments:

Post a Comment