简体   繁体   中英

Bit Reversal - not clear what the output is

I am reading the following example :

  Var1(REG1, 0U, 16U);
  Var2(REG2, 0U, 8U);

   UINT32 FirstReg = Getaddress1(Var1); //the dimension is 16 bit

   FirstReg = ((FirstReg >> 1) & 0x5555) | ((FirstReg << 1) & 0xaaaa);
   FirstReg = ((FirstReg >> 2) & 0x3333) | ((FirstReg << 2) & 0xcccc);
   FirstReg = ((FirstReg >> 4) & 0x0f0f) | ((FirstReg << 4) & 0xf0f0);
   FirstReg = ((FirstReg >> 8) & 0x00ff) | ((FirstReg << 8) & 0xff00);
   FirstReg = (FirstReg << 8);

   UINT32 SecondReg = Getaddress2(Var2);//the dimension is 8 bit
   SecondReg = ((SecondReg >> 1) & 0x5555) | ((SecondReg << 1) & 0xaaaa);
   SecondReg = ((SecondReg >> 2) & 0x3333) | ((SecondReg << 2) & 0xcccc);
   SecondReg = ((SecondReg >> 4) & 0x0f0f) | ((SecondReg << 4) & 0xf0f0);
   SecondReg = ((SecondReg >> 8) & 0x00ff) | ((SecondReg << 8) & 0xff00);
   SecondReg = (SecondReg >> 8);

   return (FirstReg | SecondReg);

Basically as far as i undestand the intention is to reverse the bits read in the 2 UINT32 Reg(s) variables and collect in only 1 variable of UINT32 type.

I don't get if the first bit (for example) of SecondReg will become the 17th bit of the returned variable or the first one.

First, even if the algorythm works with 32 bits integers, only the 16 least significant bits are used because they are anded with 16 bits only values.

So after the first part (before the last shift) FirstReg and SecondReg contains the 16 least significant bits of the original values reversed.

Then FirstReg is shifted left 8 bits and SecondReg is shifted right 8 bits and both are ored . The result is a 32 bits values composed with (most significant byte to least): O, high order byte of FirstReg , low order byte of FirstReg , high order byte of SecondReg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM