help needed

xlt_bit_swap(const unsigned char in[], long len, unsigned char out[])
{
for (long i = 0; i < len; i++)
*(out++) = ((*in) << 4) | ((*in++) >> 4);
}

Please any one explain me how the following code works.

Thanks in advance
This function writes len characters from a character array pointed by in into a character array pointed by out swapping high 4 bits and low 4 bits of each character.
as vlad said: xlt_bit_swap will swap higher and lower 4 bits:

putting it simply:
if:
*in == 0x61 which is 0110 0001 binary
then
*out = 0x37 which is 0001 0110
Last edited on
To understand what tthe function does you can try the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>

int main()
{
   unsigned char c = 'A';

   std::cout << std::hex << int( c ) << std::endl;
   std::cout << int( unsigned char( c << 4 ) ) << std::endl;
   std::cout << int( unsigned char( c >> 4 ) ) << std::endl;
   std::cout << ( int( unsigned char( c << 4 | c >> 4 ) ) std::endl;

   return 0;
}


It will be compiled if you are using MS VC++. However GCC 4.7.2 will not compile the code because it seems that it contains a bug.:)

So one (and I think the most important) reason why C++ is difficult to learn is that all C++ compilers contain bugs and do not satisfy the C++ Standard.:)

EDIT: I am sorry. It seems that it is the MS VC++ compiler that contains the bug. In functional notation of expression conversions only a simple type specifier shall be used . So for example unsigned and unsigned int are not the same in all situations.:)
Last edited on
Topic archived. No new replies allowed.