Solved

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

Posted on 2006-11-14
11
228 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:tatikor_143
  • 5
  • 3
  • 3
11 Comments
 
LVL 6

Expert Comment

by:billtouch
ID: 17942052
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
0
 

Author Comment

by:tatikor_143
ID: 17942569
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 17942587
CHAR_BIT as defined in limits.h is the number of bits for the smallest object that is not a bitfield in your implementatiion
0
 
LVL 84

Expert Comment

by:ozo
ID: 17942610
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)
0
 
LVL 6

Accepted Solution

by:
billtouch earned 500 total points
ID: 17944726
A small program is what you have presented. Let me try to help you understand it better.

In c, the << and >> are bitwise operators. The statement: a << b causes the value in a to be shifted left by b bytes.

Example:
a = 00000010;
b = 2;

c = a << b;

then c will contain the value: 00001000;

You will notice the bit has moved two places to the left. The value of c is a * 2 to the bth power ( a( 2^b) ).

With the >> operator, the bit will move to the right.

If a bit is on the edge (left edge for << or right edge for >>), it will be shifted out and lost. Zeros are shifted into the opposite end.
so if you had a 16 bit integer shifted in either direction 16 bits, the value left in that integer will be zero.

Thus:
char c;  // An 8 bit integer

c = 0xFF // binary 11111111
for ( i=0; i<8; i++ )
{
  c  << 1;
  printf( "%x\n", c ;
}

will print:
7f                     ( 0111 1111 )
3f                     ( 0011 1111 )
1f                     ( 0001 1111 )
0f                     ( 0000 1111 )
07                    ( 0000 0111 )
03                    ( 0000 0011 )
01                    ( 0000 0001 )
00                    ( 0000 0000 )

Do you see how the bitwise operators work now?

Then take that and expand it to a 32 bit word and instead of shifting left or right one bit, you shift one byte or 8 bits. Now you have how your model works.

By the way... the bug in your example is not in the bitwise manipulations. Try to figure it out. It is a common c mistake.

Bill

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Expert Comment

by:billtouch
ID: 17947193
Ok - I just reread this when I was awake. In the code snipit I gave, the "c << 1;" should be  "c>>=1;".

Sorry!
Bill
0
 

Author Comment

by:tatikor_143
ID: 17947952
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
0
 
LVL 6

Expert Comment

by:billtouch
ID: 17951145
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 17951287
+= 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.
0
 
LVL 6

Expert Comment

by:billtouch
ID: 17951403
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
0
 

Author Comment

by:tatikor_143
ID: 17951686
Thanks bill,for your support.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now