How to extract bytes from long?

P

pete

Samuel said:
The correct usage is therefore ~0u or ~0U.

Bitwise operators are usually used on unsigned types.

Bitwise operators return implementation defined values
or cause undefined behavior, on signed types,
if the sign bit is set or if the operation attempts to set it.
 
P

Peter Shaggy Haywood

Groovy hepcat CBFalconer was jivin' on Thu, 16 Oct 2003 06:07:04 GMT
in comp.lang.c.
Re: How to extract bytes from long?'s a cool scene! Dig it!
You need neither CHAR_BIT nor shifts nor limits.h nor sizeof:

for (i = 8; i > 0; --i) {
printf("%x ", value % 256);
value /= 256;
}
putchar('\n'); /* <--AND HERE is where the \n goes */
return 0;
}

and the result is portable.

Hardly. You are making huge assumptions about the sizes of long and
unsigned char. Who says sizeof(long) is 8? And who says a byte is 8
bits? These assumptions are evident in your code, and are decidedly
non-portable.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
C

CBFalconer

Peter said:
CBFalconer was jivin' on Thu, 16 Oct 2003 06:07:04 GMT


Hardly. You are making huge assumptions about the sizes of long and
unsigned char. Who says sizeof(long) is 8? And who says a byte is 8
bits? These assumptions are evident in your code, and are decidedly
non-portable.

But it is, if you say the object is to emit 8 pairs of hex digits,
with the first representing the least significant bits. This
gives a fixed output field. Conceded, it truncates anything past
64 bits in an unsigned long.

If the objective is to dump all digits, and ignore the more
significant zeroes, you can simply exit the loop when the value
becomes zero.

In both cases the technique is independant of CHAR_BIT and
sizeof(long) and, most important, independent of endianess.

We could detect the bits needed by a combination of CHAR_BIT and
sizeof(long), possibly also examining ULONG_MAX, but the result
would no longer be a fixed field. The question is: What is
wanted.
 
P

pete

Why not just:
puts("0x78, 0x56, 0x34, 0x12");
?
That's portable too.
But it is, if you say the object is to emit 8 pairs of hex digits,

Where did you get the idea that the object,
is to emit 8 pairs of hex digits?
Nothing like that, has even been remotely suggested
anywhere previously in this thread.
with the first representing the least significant bits. This
gives a fixed output field. Conceded, it truncates anything past
64 bits in an unsigned long.

If the objective is to dump all digits, and ignore the more
significant zeroes, you can simply exit the loop when the value
becomes zero.

In both cases the technique is independant of CHAR_BIT and
sizeof(long) and, most important, independent of endianess.

The technique is also independant of, and irrelevant to,
the the subject line of this thread.
OP gave an example which suggested 8 bits per byte,
but the question was "How to extract bytes from long?"
 
C

CBFalconer

pete said:
.... snip ...


Where did you get the idea that the object,
is to emit 8 pairs of hex digits?
Nothing like that, has even been remotely suggested
anywhere previously in this thread.

From the original post, quoted below:
How to extract bytes from long, starting from the last byte?
For example, I have a long number:
0x12345678
I need to represent it as the following bytes list:
0x78, 0x56, 0x34, 0x12

Granted, it is not definitive, but is how I interpreted it.
 
P

pete

Samuel said:
I was wrong about everything. ~0 is "wrong" (in terms of the abstract
C machine); the correct expression is ~0u or ~0U.

(unsigned char)~0u would work instead of
(unsigned char)-1, in the above code example,
but for the general case of casting a constant,
to get an object with all bits set,
(unsigned cast)-1 is more versatile than (unsigned cast)~0u.
((size_t)-1) and ((long unsigned)-1),
are both unsigned values with all bits set.
((size_t)~0u) and ((long unsigned)~0u),
may or may not have all bits set,
depending on whether the width of the respective types,
is greater than the width of unsigned.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top