GetByte(int x, int n)

F

Floyd

GetByte(x, 3) should return the 3rd byte of the 32 bit integer x. Allowed
operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks... and using
x&bitmask ?

Any other ideas?
 
R

Ravi Uday

Floyd said:
GetByte(x, 3) should return the 3rd byte of the 32 bit integer x. Allowed
operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks... and using
x&bitmask ?

Any other ideas?

There are a couple of others.
But, this group doesnt do home works !!
Post the solution that you have tried. We can comment and suggest different
other approaches.

- Ravi
 
J

Jason Whitehurst

Floyd said:
GetByte(x, 3) should return the 3rd byte of the 32 bit integer x.
Allowed operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks... and
using x&bitmask ?

Any other ideas?

Using resources is one thing, asking for the answer to your HW question
straight out is another.

Yes, I keep up with this newsgroup.
 
K

Keith Thompson

Jason Whitehurst said:
Using resources is one thing, asking for the answer to your HW question
straight out is another.

Yes, I keep up with this newsgroup.

In the immortal words of Homer Simpson:

"D'oh! Stupid poetic justice!"
 
P

pete

Floyd said:
GetByte(x, 3) should return the 3rd byte of the 32 bit integer x.
Allowed
operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks...
and using x&bitmask ?

Any other ideas?

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

#define GetByte(x, u) \
((x & UCHAR_MAX << u * CHAR_BIT) >> u * CHAR_BIT)

int main(void)
{
unsigned long hex1, byte;

hex1 = 0x12345678;
printf("Hex1: %#lx\n", hex1);
byte = sizeof hex1;
while (byte-- != 0) {
printf("byte %lu is %#lx\n", byte, GetByte(hex1, byte));
}
return 0;
}

/* END new.c */
 
J

jacob navia

[snip]

Why did you solve his homework for him?

Coudn't you read the other answers before throwing out
code like that???
 
C

CBFalconer

jacob said:
pete said:
Floyd wrote:
[snip]

Why did you solve his homework for him?

He didn't.

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
 
M

Merrill & Michele

[code for the rest of us who aren't cheating]
[snip]
jacob navia wrote:
Why did you solve his homework for him?

Coudn't you read the other answers before throwing out
code like that???

If he uses code similar to Pete's, he will GetBusted(). This is the
funniest thread I've read here. MPJ
 
M

Michael Mair

CBFalconer said:
jacob said:
pete said:
Floyd wrote:

[snip]

Why did you solve his homework for him?


He didn't.

Hint for Jacob: Look at the list of permitted operators ;-)
I really enjoyed Pete's answer: Standard-conforming, short,
elegant, ... and useless for the cheating OP.


Cheers
Michael
 
C

Christopher Benson-Manica

Floyd said:
Any other ideas?

Yep - if Jim Greenlee were still teaching 2130, you would be
completely toast. Good luck trying to cheat in 2340...
 
R

Richard Bos

Jason Whitehurst said:
Using resources is one thing, asking for the answer to your HW question
straight out is another.

Yes, I keep up with this newsgroup.

Nice to see swift retribution in action for a change!

Richard
 
P

pete

jacob said:
[snip]

Why did you solve his homework for him?

I don't know if it's right.
Does
"GetByte(x, 3) should return the 3rd byte of the 32 bit integer x."
mean the value of the third least significant byte of the integer value,
or the value of the third addressable byte of the integer object?

I think "3rd" usually means starting from first,
rather than from zeroeth, like I did before.

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

#define GetByte_V(x, u) \
((x & UCHAR_MAX << u * CHAR_BIT - CHAR_BIT) >> u * CHAR_BIT - CHAR_BIT)

#define GetByte_O(x, u) (((unsigned char *)&x)[u - 1])

int main(void)
{
unsigned long hex1, byte;

hex1 = 0x12345678;

printf("\nHex1: %#lx\n", hex1);
byte = 0;
do {
++byte;
printf("byte %lu is %#lx\n", byte, GetByte_V(hex1, byte));
} while (byte != sizeof hex1);

printf("\nHex1: %#lx\n", hex1);
byte = 0;
do {
++byte;
printf("byte %lu is %#lx\n", byte,
(long unsigned)GetByte_O(hex1, byte));
} while (byte != sizeof hex1);

return 0;
}

/* END new.c */
 
D

Dan Pop

In said:
GetByte(x, 3) should return the 3rd byte of the 32 bit integer x. Allowed
operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks... and using
x&bitmask ?

Any other ideas?

Your homework assignment is unclear. What is the 3rd byte of the 32 bit
integer x? Is the counting starting with the most significant byte or
the least significant byte and is the counting 0-based or 1-based?

Because you're an obvious beginner, we shall assume that a byte is 8 bits.

The idea is to bring the byte of interest in the least significant
position of the integer. This is a job for the >> operator, but you'll
have to compute the shift count yourself (even if I wanted to do that
for you, I wouldn't know how, for the reasons explained at the beginning
of my reply).

Once you have the byte there, it is trivial to get rid of all the other
bytes, by masking them away with the bitwise & operator and a suitably
chosen mask (you can trivially figure it out).

The exercise is a bit more interesting if we don't make any assumption
about the byte size. We can get the byte size from <limits.h> as the
macro CHAR_BIT. Once we have it we can build the mask with a loop, but
there are a couple of shortcuts in C: use the macro UCHAR_MAX from the
same header or use the magic expression (unsigned char)(-1).

Dan
 
D

Dan Pop

In said:
#define GetByte(x, u) \
((x & UCHAR_MAX << u * CHAR_BIT) >> u * CHAR_BIT)

Please have the minimal decency not to expose newbies to badly implemented
macros! Both x and u MUST be protected by parentheses in the expression.

Besides, you have chosen the worst algorithm.

Dan
 
D

Dan Pop

In said:
[snip]

Why did you solve his homework for him?

Coudn't you read the other answers before throwing out
code like that???

Read Pete's code carefully. No beginner could turn in such a solution
and expect to be happily accepted by the instructor. Unless the
instructor made <limits.h> one of his priorities, of course.

Dan
 
E

Eric Sosman

Dan said:
Your homework assignment is unclear. What is the 3rd byte of the 32 bit
integer x? Is the counting starting with the most significant byte or
the least significant byte and is the counting 0-based or 1-based?

Because you're an obvious beginner, we shall assume that a byte is 8 bits.

The idea is to bring the byte of interest in the least significant
position of the integer. This is a job for the >> operator, but you'll
have to compute the shift count yourself (even if I wanted to do that
for you, I wouldn't know how, for the reasons explained at the beginning
of my reply).

Once you have the byte there, it is trivial to get rid of all the other
bytes, by masking them away with the bitwise & operator and a suitably
chosen mask (you can trivially figure it out).

The exercise is a bit more interesting if we don't make any assumption
about the byte size. We can get the byte size from <limits.h> as the
macro CHAR_BIT. Once we have it we can build the mask with a loop, but
there are a couple of shortcuts in C: use the macro UCHAR_MAX from the
same header or use the magic expression (unsigned char)(-1).

It'll have to be UCHAR_MAX, I think, because the list
of permitted operators doesn't include casts.

The permitted operators seem to have been chosen so as
to steer the student away from the byte-extraction solutions
that any normal programmer would use, and towards solutions
that good programmers would avoid. Consider:

- Multiplication is forbidden, making it hard to compute
a shift distance from a byte number.

- The array-indexing operator `[]' is forbidden, so the
shift distance can't even be fetched from a pre-
computed table.

- The prohibition on assignment rules out iterative
methods, and multi-step methods in general.

Fortunately, the problem statement only requires that
the code work for "the 3rd byte;" there is no requirement
on what it should do if the second argument is not 3. This
leads to a simple solution, once the counting ambiguity Dan
points out is resolved. Here's what it might look like
if we assume that the least significant byte is the first,
the next-higher byte is the second, and so forth:

#include <limits.h>
unsigned char GetByte(long x, int ignored) {
return (x & (UCHAR_MAX << (CHAR_BIT + CHAR_BIT))
>> (CHAR_BIT + CHAR_BIT);
}

Note 1: It's clumsy to mask before shifting rather than
afterward, but if `x' is negative right-shifting it is
problematic.

Note 2: From the problem statement we know that `x' has
exactly 32 bits and that it contains at least three bytes,
hence CHAR_BIT < 11. From the C Standard we know that
CHAR_BIT is at least 8 and that it is a divisor of the
number of bits in `x'. Putting these together, we can
conclude that CHAR_BIT is exactly 8 for the purposes of
this problem, and this permits some simplification:

/* <limits.h> no longer required */
unsigned char GetByte(long x, int ignored) {
return (x & 16711680) >> '\020';
}

No sane programmer would write such crud, but I think
it solves the problem as stated.
 
E

Eric Sosman

Eric said:
[...]
#include <limits.h>
unsigned char GetByte(long x, int ignored) {
return (x & (UCHAR_MAX << (CHAR_BIT + CHAR_BIT))

Sorry; I left out a ')' here.
 
T

Thomas Matthews

Floyd said:
GetByte(x, 3) should return the 3rd byte of the 32 bit integer x. Allowed
operators: ! ~ & ^ | + << >> (no assignment!).

Would the easiest way to do this be just creating 4 bit masks... and using
x&bitmask ?

Any other ideas?

By the way, have you considered Endianism into the fray?

Also, what happens when the byte number is 0, 4, 5, 27689?
Is modulo arithmetic performed before accessing the byte?
Is an error returned?

How does one differentiate an returned error value from
a possible value? For example, zero is a possible value,
so is -1, both of which are classic error values.

Hmmm, the assignment needs better definition.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Merrill & Michele

Dan Pop wrote
Your homework assignment is unclear. What is the 3rd byte of the 32 bit
integer x? Is the counting starting with the most significant byte or
the least significant byte and is the counting 0-based or 1-based?

Because you're an obvious beginner, we shall assume that a byte is 8 bits.

The idea is to bring the byte of interest in the least significant
position of the integer. This is a job for the >> operator, but you'll
have to compute the shift count yourself (even if I wanted to do that
for you, I wouldn't know how, for the reasons explained at the beginning
of my reply).

Once you have the byte there, it is trivial to get rid of all the other
bytes, by masking them away with the bitwise & operator and a suitably
chosen mask (you can trivially figure it out).

The exercise is a bit more interesting if we don't make any assumption
about the byte size. We can get the byte size from <limits.h> as the
macro CHAR_BIT. Once we have it we can build the mask with a loop, but
there are a couple of shortcuts in C: use the macro UCHAR_MAX from the
same header or use the magic expression (unsigned char)(-1).

Now that everyone's had their fun with Floyd's discomfiture, and the thread
is starting to drift, I wanted to ask a *somewhat* similar question, in that
it involves bit manipulations. I do not, however, have a T.A.

In K&R2 §2.9 appears the following sentence: The bitwise OR operator | is
used to turn bits on:
x=x | SET_ON;
sets to one in x the bits that are set to one in SET_ON.

I've looked in the index and the usual header files, and I find nothing on
SET_ON. I am no longer subject to the admonition to read K&R sequentially,
as I am doing so presently and am past this point. MPJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top