473,408 Members | 2,161 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,408 software developers and data experts.

What's wrong with this code?

It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?

Sep 29 '07 #1
8 2803
"xbyte" <ji********@gmail.comschrieb im Newsbeitrag
news:11**********************@50g2000hsm.googlegro ups.com...
It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?
The function should return a byte (unsigned char), but does return an int.
You'd need to check whether bytenum is in the range of 0-3
There's no quarantee that unsigned (int) is 32 bit long

Bye, Jojo
Sep 29 '07 #2

"xbyte" <ji********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?
The programmer might have failed the course, but the code is good enough to
be used even in a production environment. Because it is extremely unlikely
that it can fail usefully.

However bytenum should be checked to be within the range 0-3, word should
arguably be an unsigned long to guarantee at least 32 bits. I say "arguably"
because in fact an extremely low level function like this needs to be
efficient, and a conversion from int to long might be exactly what you don't
need if it is not a nop.
The identifier "word" is a bit unfortunate, since it looks like it might be
a user-defined basic type.

int is the conventional return type for functions returning chars, by the
way. You could argue this is a bad convention, but it allows -1 on error.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 29 '07 #3
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Malcolm McLean wrote, On 29/09/07 21:03:
>>
"xbyte" <ji********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegr oups.com...
>>It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?
The programmer might have failed the course, but the code is good
enough to be used even in a production environment.

Depends on the environment. I can certainly see it being rejected at
review for using a left shift where what is really meant is
multiplication.
Not if we are talking about shifting as opposed to multiplication. Using
the shift makes it obvious that we are shifting bits around. "*8" on the
other hand might be any arbitrary multiplication that just happens to be
a power of 2.
Sep 29 '07 #4
Richard wrote:
) Flash Gordon <sp**@flash-gordon.me.ukwrites:
)Malcolm McLean wrote, On 29/09/07 21:03:
)>>
)>"xbyte" <ji********@gmail.comwrote in message
)>news:11**********************@50g2000hsm.googleg roups.com...
)>>It's a problem from a book, and I can't figure it out.
)>>>
)>>4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
)>>you've gotta extract an indicated (by bytenum) word from it, AND the
)>>following code is said to be written by a failed programmer:
)>>>
)>>int xbyte(unsigned word, int bytenum)
)>>{
)>> return (word >(bytenum << 3)) & 0xFF;
)>>}
)>>>
)>>IMHO, this is a perfect bulk of code, what's your opinion?
)>>>
)>The programmer might have failed the course, but the code is good
)>enough to be used even in a production environment.
)>
)Depends on the environment. I can certainly see it being rejected at
)review for using a left shift where what is really meant is
)multiplication.
)
) Not if we are talking about shifting as opposed to multiplication. Using
) the shift makes it obvious that we are shifting bits around. "*8" on the
) other hand might be any arbitrary multiplication that just happens to be
) a power of 2.

I direct your attention to what the code actually tries to do.
8 *is* an arbitrary multiplication that happens to be a power of two.
It could just as well have been 9.

To make the point more clear, a slightly more portable way to write
it would be:

return (word >(bytenum * CHAR_BIT)) & UCHAR_MAX;

Although I'm sure there are several issues with that as well.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Sep 29 '07 #5
On Sun, 30 Sep 2007 01:32:04 +0200, Richard wrote:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>Malcolm McLean wrote, On 29/09/07 21:03:
>>>
"xbyte" <ji********@gmail.comwrote in message
news:11**********************@50g2000hsm.googleg roups.com...
It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum) {
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?

The programmer might have failed the course, but the code is good
enough to be used even in a production environment.

Depends on the environment. I can certainly see it being rejected at
review for using a left shift where what is really meant is
multiplication.

Not if we are talking about shifting as opposed to multiplication. Using
the shift makes it obvious that we are shifting bits around. "*8" on the
other hand might be any arbitrary multiplication that just happens to be
a power of 2.
If you were writing the code for a 9-bit char 36-bit int machine, you
might end up with something like

int xbyte(unsigned word, int bytenum) {
return (word >(bytenum * 9)) & 0x1FF;
}

"*8" is just any arbitrary multiplication that happens to be a power of
2, in this case.
Sep 29 '07 #6
Willem wrote, On 30/09/07 00:45:
Richard wrote:
) Flash Gordon <sp**@flash-gordon.me.ukwrites:
)Malcolm McLean wrote, On 29/09/07 21:03:
)>>
)>"xbyte" <ji********@gmail.comwrote in message
)>news:11**********************@50g2000hsm.googleg roups.com...
<snip>
)>> return (word >(bytenum << 3)) & 0xFF;
<snip>
)>The programmer might have failed the course, but the code is good
)>enough to be used even in a production environment.
)>
)Depends on the environment. I can certainly see it being rejected at
)review for using a left shift where what is really meant is
)multiplication.
)
) Not if we are talking about shifting as opposed to multiplication. Using
) the shift makes it obvious that we are shifting bits around. "*8" on the
) other hand might be any arbitrary multiplication that just happens to be
) a power of 2.

I direct your attention to what the code actually tries to do.
8 *is* an arbitrary multiplication that happens to be a power of two.
It could just as well have been 9.
Yes, that is indeed a correct expansion of my point, as was Harold's
response to Richard.
To make the point more clear, a slightly more portable way to write
it would be:

return (word >(bytenum * CHAR_BIT)) & UCHAR_MAX;

Although I'm sure there are several issues with that as well.
It depends on the real requirement. If you are dealing with some format
where you have four 8 bit numbers packed in to an integer type then
using 8 and 0xFF is correct, if the requirement is 4 C bytes then your
code is correct.
--
Flash Gordon
Sep 30 '07 #7
On Sat, 29 Sep 2007 08:43:44 -0700, xbyte wrote:
It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?
None. But if you replace `<< 3` with `* CHAR_BIT` and 0xFF with
UCHAR_MAX (these are in <limits.h>), you don't have to know how
big the word and the bytes are.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 30 '07 #8
On 2007-09-30 12:40, Army1987 <ar******@NOSPAM.itwrote:
On Sat, 29 Sep 2007 08:43:44 -0700, xbyte wrote:
>It's a problem from a book, and I can't figure it out.

4 words (4x8 bits) are packed into a 32-bit unsigned integer, and
you've gotta extract an indicated (by bytenum) word from it, AND the
following code is said to be written by a failed programmer:

int xbyte(unsigned word, int bytenum)
{
return (word >(bytenum << 3)) & 0xFF;
}

IMHO, this is a perfect bulk of code, what's your opinion?
None. But if you replace `<< 3` with `* CHAR_BIT` and 0xFF with
UCHAR_MAX (these are in <limits.h>), you don't have to know how
big the word and the bytes are.
Except that the requirement explicitely says "4x8 bits", not 4 x
CHAR_BIT bits. That may be an error in the requirement, but more likely
it is mandated by some external source, like a file format or a
communication protocol (in the real world - I have seen that this
problem is from "a book"). Then * 8 and & 0xFF will work correctly even
on machines with larger bytes, but * CHAR_BIT and UCHAR_MAX will not.

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hj*@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Sep 30 '07 #9

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.