How to extract bytes from long?

R

RB

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

Thanks in advance,
Rita
 
E

Ed Morton

RB said:
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

Thanks in advance,
Rita

Sounds a bit homework-y, but as a hint try this:

#include <stdio.h>

int main(void)
{
long val=0x12345678;

printf("0x%x\n",val >> 16 & 0xff );

return 0;
}

Regards,

Ed.
 
N

Nils Petter Vaskinn

How to extract bytes from long, starting from the last byte?

#include <stdio.h>

int main() {

unsigned long value = 0x12345678;
int i;

printf("%#lx\n",value);

for (i = sizeof value; i > 0; --i) {
printf("%#x\n", value & 0xff);
value >>= 8;
}
return 0;
}
 
T

Thomas Stegen

RB said:
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

Thanks in advance,
Rita

Look at bitwise operators. &, |, ^, <<, >>. Then look up bitmasks
and how to use them and everything should be rather straightforward.
 
P

pete

#include said:
#include <stdio.h>

int main() {

unsigned long value = 0x12345678;
int i;

printf("%#lx\n",value);

for (i = sizeof value; i > 0; --i) {
printf("%#x\n", value & 0xff);
value >>= 8;

/*
** You realise that you don't know the size of value,
** so you might as well go all the way.
*/
printf("%#x\n", value & (unsigned char)-1);
value >>= CHAR_BIT;
 
P

pete

.... and now for the (sizeof(long)==1) portable way:

/* BEGIN new.c */

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

int main(void)
{
unsigned long value = 0x12345678;
size_t i;

printf("%#lx\n",value);
printf("%#x\n", value & (unsigned char)-1);
for (i = sizeof value - 1; i != 0; --i) {
value >>= CHAR_BIT;
printf("%#x\n", value & (unsigned char)-1);
}
return 0;
}

/* END new.c */

and now for the way which interprets "last byte"
as being the one furthest from ((char*)&value)

/* BEGIN new2.c */

#include <stdio.h>

int main(void)
{
unsigned long value = 0x12345678;
unsigned char *pointer = (unsigned char*)&value + sizeof value;

printf("%#lx\n",value);
do {
--pointer;
printf("%#x\n", (unsigned)*pointer);
} while (pointer != (unsigned char*)&value);
return 0;
}

/* END new2.c */
 
J

John Bode

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

Thanks in advance,
Rita

There are a couple of ways. The safest is to use a bitmask (0xFF),
the bitwise & operator, and the >> and << shift operators.
Alternately, you can treat the long as an array of unsigned char by
creating an unsigned char pointer and setting it to the same address
as the long (unsigned char *p = (unsigned char *) &mylong;) and then
either use array subscript notation to access individual bytes or
"walk" the array by incrementing p, but you have to be aware of
endianness issues (i.e., on a little-endian machine, p[0] would be the
LSB, whereas on a big-endian machine it would be the MSB). The first
method (bitmask and shift) works the same regardless of endian issues.
 
P

Peter Nilsson

pete said:
... and now for the (sizeof(long)==1) portable way:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* BEGIN new.c */

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

int main(void)
{
unsigned long value = 0x12345678;
size_t i;

printf("%#lx\n",value);
printf("%#x\n", value & (unsigned char)-1);
for (i = sizeof value - 1; i != 0; --i) {
value >>= CHAR_BIT;

UB if sizeof(long) == 1.

value = value >> (CHAR_BIT - 1) >> 1;
 
C

CBFalconer

pete said:
/*
** You realise that you don't know the size of value,
** so you might as well go all the way.
*/
printf("%#x\n", value & (unsigned char)-1);
value >>= CHAR_BIT;

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.
 
S

Samuel Barber

pete said:
printf("%#x\n", value & (unsigned char)-1);
for (i = sizeof value - 1; i != 0; --i) {
value >>= CHAR_BIT;
printf("%#x\n", value & (unsigned char)-1);
}
return 0;
}

I would use ~0 for "all 1s" rather than -1.

Sam
 
S

Samuel Barber

That risks a trap representation under C99.

Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.

Sam
 
P

pete

CBFalconer said:
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.

I was addressing the more general subject,
in the subject line of this thread: "How to extract bytes from long?",
rather than how to extract bytes from 0x12345678
or any other number which doesn't require more than 32 bits.
 
P

pete

Samuel said:
Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.

You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.

-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.
 
S

Sheldon Simms

You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.

~0u cannot be a trap representation, however.

6.5.3.3
4 The result of the ~ operator is the bitwise complement of its
(promoted) operand ... If the promoted type is an unsigned type,
the expression ~E is equivalent to the maximum value representable
in that type minus E.
 
S

Samuel Barber

pete said:
You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.

You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations. How can a bitwise operation trap? It can't.
-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.

Hello? The point is that -1 is ***being used as*** a bit pattern. The
intent is to get "all 1s", which is true if the integer representation
is 2's complement; that's an implicit assumption of the code. (This is
the best reason not to use -1: the intent is not perfectly clear).

Sam
 
A

Arthur J. O'Dwyer

You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations.

It is AFAIK implementation-defined whether the concept of "negative
zero" is meaningful in standard C (depending on the representation
of signed integers).
How can a bitwise operation trap? It can't.

Of course it can! (Why wouldn't it? And do modern digital computers
perform any operations that *aren't* bitwise, anyway?)

To clarify pete's point:

(uchar)-1 == (uchar)-((int)1) == (-(int)1)+UCHAR_MAX == UCHAR_MAX-1

....which is guaranteed to have an all-ones bit pattern, regardless
of padding or n's-complement.

Hello? The point is that -1 is ***being used as*** a bit pattern.

Then it's being used incorrectly. -1 is *not* a bit pattern, it's
an integer expression equal to the additive inverse of the integer 1.
It doesn't have a "bit pattern" per se.
The intent is to get "all 1s", which is true if the integer
representation is 2's complement; that's an implicit assumption
of the code.

If that *were* an implicit assumption of the code, then the code
would be broken. But it's not. Pete's code AFAICT doesn't assume
anything earth-shattering about the integer representations used
by the target system.
(This is the best reason not to use -1: the intent is not
perfectly clear).

However, it *does* produce the right answer, which is a point in
its favor. ~0 might trap, and in any case I think (unsigned char)-1
has a bit more aesthetic value to it (YMMV, of course).

And if you want everything to be *perfectly* clear, then you might
want to consider a different programming language. C is just
full of 'for (i=0; a; ++i)'s and 'while (*s++ = *t++)'s; if
a simple -1 throws you, then you're in trouble. :)

-Arthur
 
S

Sheldon Simms

You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations. How can a bitwise operation trap? It can't.

6.2.6.2 Integer types
2 ... (It is implementation-defined) whether the value with ...
sign bit 1 and all value bits 1 (for one's complement), is a trap
representation or a normal value. In the case of ... one's
complement, if this representation is a normal value it is called
a negative zero.
...
3 If the implementation supports negative zeros, they shall be
generated only by: the &, |, ^, ~, <<, and >> operators with
arguments that produce such a value;
...
4 If the implementation does not support negative zeros, the
behavior of the &, |, ^, ~, <<, and >> operators with arguments
that would produce such a value is undefined.
 
J

Jirka Klaue

Arthur J. O'Dwyer wrote:
....
(uchar)-1 == (uchar)-((int)1) == (-(int)1)+UCHAR_MAX == UCHAR_MAX-1

You must have miscalculated somewhere. (uc)-1 should be UCHAR_MAX.

(uc)-1 == (uc)(0 - 1) == (uc)(UINT_MAX + 1 - 1) == (uc)(UINT_MAX) == UCHAR_MAX

Jirka
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top