Link to home
Start Free TrialLog in
Avatar of tatikor_143
tatikor_143

asked on

Can anyone help me to explain me the small code snippet below?

Can anyone help me to explain the following code snippet.This program is used to convert  bits from Big endian to little endian.What is CHAR_BIT?It has not been declared anyewhere and what right takes as a paramater.If possible can you explain the below lines with a small example.

#include <limits.h>
unsigned unsigned int reversebytes(unsigned int right) // Please write your comments here.
{
  int left=0;
  int i;
  for( i=0;i<sizeof(int); i++ ){
    left <<= CHAR_BIT; //
    left += right&(1<<CHAR_BIT)-1; //
    rignt >>= CHAR_BIT; //
  }
  return left;
}

 Eagerly waiting for the reply.
Avatar of billtouch
billtouch
Flag of United States of America image

This isn't a well written function. I will let you discover the error here.

Let's do a walk through and start with some assumptions.

1) let us assume that sizeof(int) = 4 (32 bits).
2) CHAR_BIT appears to be number of bits in a char (8).
3) The object here looks like it will input ABCD and return DCBA.

Each loop will take one group of CHAR_BIT (8) bits out of right and place it into left.

It prepares for the next iteration by shifting the remaining bits in right down 8 bits and shifting the value in left up by 8 bits.
You will see right and left looking like:
right    |  left
ABCD | ----
-ABC   | ---D
--AB    | --DC
---A      | -DCB
----       | DCBA
After i reaches sizeof(int), the value in left is returned.

right has no arguments as it is itself an argument.

Can you see the mistake in this function?

Bill
Avatar of tatikor_143
tatikor_143

ASKER

Hi Bill,

I am sorry to say that I am still confused.I am bit poor in bitwise programming.Can you please explain me the above one with small program.

Suppose if I give value as 10.How it will be stored in Big endian format and little endian format and how the bits are swapped from the above program?


A prompt response is highly appreciated.

Thanks in advance
Avatar of ozo
CHAR_BIT as defined in limits.h is the number of bits for the smallest object that is not a bitfield in your implementatiion
the number 10 in binary is 1010
if a 16 bit value is stored in two 8 bit bytes,
it will be either
00000000 00001010
if the high order byte comes first (bigendian) or
00001010 00000000
if the low order byte comes first (littleendian)
ASKER CERTIFIED SOLUTION
Avatar of billtouch
billtouch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok - I just reread this when I was awake. In the code snipit I gave, the "c << 1;" should be  "c>>=1;".

Sorry!
Bill
Hi Bill,

Thanks for the detailed explanation on the bitwise operators.Now I am compleately aware of the logic used in the above programb but there is a small mistake in the above one.I guess it is

1) left += right&(1<<CHAR_BIT)-1 // -1 should be placed inside the brackets so that it evaluates to 255 and become 11111111 so that it swap the bytes to the left with the same value.

Please let me know if I am right.

Thanks
Actually, that is correct. It would have been less cryptic if it were in parenthesis.

left += right&(1<<CHAR_BIT)-1

First notice the &. Since you are dealing with 8 bit bytes, you would want to mask off the lower 8 bits of right. So you would write
(right & 0xff).
This is written to accomodate a variable byte size (almost never  heard of anymore).
In all machines that  have 8 bit bytes the value of CHAR_BIT is 8. Thus the expression (1<<CHAR_BIT) is (1<<8) or 0x100.
You want this to be a mask so you subtract one and you get your 0xff.
So that expression is equivalent to (right & 0xff). Putting the -1 in brackets would give different results.

That is not the error. I might add that there is a syntax error as well (unsigned unsigned).

The error is: int left. The function takes in an unsigned and returns an unsigned, both 32 bits of significance and uses a signed left with 31 bits of significance. That makes a difference because they use an += instead of a |=.

Bill
+= won't  be a problem with a signed int, but the <<= could be
It looks like one of the 'unsigned' qualifiers got moved from where it was supposed to be.
You are right - I missppoke there. After I posted the last remark it dawned on me that the misplaced unsigned was probably a victim of being copied.

But using a bitwise operator (|= ) is more appropriate than an arithmetic operator (+=) in this context.

Bill
Thanks bill,for your support.