473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Code understanding problem

HI all:

I can not understand the following code , could explain to me in some
easy way:

unsigned long reflect (unsigned long crc, int bitnum) {

// reflects the lower 'bitnum' bits of 'crc'

unsigned long i, j=1, crcout=0;

for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
`````````````````````````````````````````````````` ``````````````
if (crc & i) crcout|=j;
````````````````````````````
j<<= 1;
}
return (crcout);
}

is the code provide the function - changeing the "11100011" to
"11000111"

Thanks for any comments

Nov 30 '05 #1
2 1234
In article <11*********************@o13g2000cwo.googlegroups. com>,
yezi <ye*****@hotmail.com> wrote:
I can not understand the following code , could explain to me in some
easy way: unsigned long reflect (unsigned long crc, int bitnum) { // reflects the lower 'bitnum' bits of 'crc' unsigned long i, j=1, crcout=0; for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
} is the code provide the function - changeing the "11100011" to
"11000111"


The part about 'crc' is irrelevant.

The routine is given an input number, and the number of bits to operate on.
It produces an output which is the lower (least signifcant
value) that-many bits of the input number, but in reverse order.

For example, if bitnum were 4, and the input were binary ...XYZW then
it would first test X (1<<3) and if it were set then it would set
the last bit in the output number, which would become ....000X .
In the next iteration, it would test the next bit rightwards, Y, (1<<2)
and if it were set then it would set the second-last bit in the
output number (1<<1), making the output ...00YX. Next iteration,
next bit rightwards, Z, (1<<1), sets third-last bit (1<<2) in the
output number, getting ...0ZYX. The last iteration, it would test
the last input bit (1<<0) and appropriately set the fourth-last
bit (1<<3) in the output number, getting ...WZYX .
--
Prototypes are supertypes of their clones. -- maplesoft
Nov 30 '05 #2
"yezi" <ye*****@hotmail.com> writes:
HI all:

I can not understand the following code , could explain to me in some
easy way:

unsigned long reflect (unsigned long crc, int bitnum) {

// reflects the lower 'bitnum' bits of 'crc'

unsigned long i, j=1, crcout=0;

for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}


First look up the operators <<, >>=, <<=, &, and |=.

Then start by looking what the values of i and j will
be each iteration:

i = (unsigned long)1<<(bitnum-1) means:
Take 1 (one), and shift it left (bitnum - 1) steps. If bitnum = 8
it is 1 << 7 which in binary is: 1000 0000. j starts at 1 (one), or
in binary: 0000 0001

After each iteration i is shifted right one step, and j shifted left
one step. So i and j will take the following values for bitnum = 8:

i | j
----------+----------
1000 0000 | 0000 0001
0100 0000 | 0000 0010
0010 0000 | 0000 0100
0001 0000 | 0000 1000
0000 1000 | 0001 0000
0000 0100 | 0010 0000
0000 0010 | 0100 0000
0000 0001 | 1000 0000

In the body of the loop there is a test if the i bit of the crc
is set, and if so set the j bit of the return value.

Personally I'd make bitnum unsigned too, check that the bitnum
value us reasonable, skip the cast, init both i and j in the
for statement, and also shift both i and j there.

/Niklas Norrthon
Dec 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
12
by: Torbjørn Pettersen | last post by:
I'm having problems validating my HTML code because of some ASP code I'm using: ---Start Code--- <% If rs("Average") = 0 Then VotingImage = "<img src="/images/0.gif" alt='No votes yet'>"...
7
by: laura | last post by:
I'm trying to understand fully Server.MapPath. I am writing an intranet for a small company and want to be able to put some files accessible to all, hyperlinked from the intranet and therefore,...
2
by: drife | last post by:
Hello, I am looking for suggestions for how I might optimize my code (make it execute faster), and make it more streamlined/ elegent. But before describing my code, I want to state that I am...
10
by: Al Christoph | last post by:
Please forgive me if this is the wrong place to post this. The last place I posted got me a fairly rude response. I guess vb.db people just don't want to think about XML as database. At any rate,...
8
by: John E Katich | last post by:
When attempt to use the Event Wizard I get the following error message: "Add/Remove of the function impossible, because the parent class code is read only" The Project was convert from VC 6.0....
6
by: Chad Z. Hower aka Kudzu | last post by:
I want to do this. I want my programmers to do all the code. All of it - run at server and run at client. I then want a graphic artist to make the look and the layout of the pages. The...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
13
by: Kirk McDonald | last post by:
Say I have a database containing chunks of Python code. I already have a way to easily load, edit, and save them. What is slightly baffling to me is how I can effectively pass this code some...
8
by: boki_pfc | last post by:
Hi Everybody, I am looking for an advice on following: I have that "pleasure" of reading C++ codes that have been written by person(s) that have not attended the same C++ classes that I did or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.