views:

177

answers:

5

I have a 32 bit long variable, CurrentPosition, that I want to split up into 4, 8bit characters. How would I do that most efficiently in C? I am working with an 8bit MCU, 8051 architectecture.

unsigned long CurrentPosition = 7654321;
unsigned char CP1 = 0;
unsigned char CP2 = 0;
unsigned char CP3 = 0;
unsigned char CP4 = 0;
// What do I do next? 

Should I just reference the starting address of CurrentPosition with a pointer and then add 8 two that address four times?

It is little Endian.

ALSO I want CurrentPosition to remain unchanged.

A: 
CP1 = (unsigned char)(CurrentPosition & 0xFF);
CurrentPosition >>= 8;
CP2 = (unsigned char)(CurrentPosition & 0xFF);
...
Matt Greer
This would change the value in CurrentPosition right? I want to leave CurrentPosition unchanged.
Jordan S
Yes it would, one option is to make a copy of CurrentPosition and mangle the copy. Or nos's answer is another option. Your idea of using a pointer is also possible. There's many ways to do this.
Matt Greer
+4  A: 
    CP1 = (CurrentPosition & 0xff000000UL) >> 24;
    CP2 = (CurrentPosition & 0x00ff0000UL) >> 16;
    CP3 = (CurrentPosition & 0x0000ff00UL) >>  8;
    CP4 = (CurrentPosition & 0x000000ffUL)      ;

You could access the bytes through a pointer as well,

unsigned char *p = (unsigned char*)&CurrentPosition;
//use p[0],p[1],p[2],p[3] to access the bytes.
nos
1)Do I need to cast these with (unsigned char)?2)Does this leave CurrentPosition unaffected?
Jordan S
1. The former will need casting2. Both methods leave CurrentPosition unaffected
Saul Rennison
The former doesn't *need* casting, but some compilers will give a bogus warning about a possible loss of precision if you don't.
caf
+1  A: 

If You are using an 8 bit MCU shifting a whole 32 bit variable is a bit of work. In this case it's better to read 4 bytes of CurrentPosition using pointer arithmetic. The cast:

unsigned char *p = (unsigned char*)&CurrentPosition;

doesn't change the CurrentPosition, but if You try to write to p[0] You will change the least significant byte of the CurrentPosition. If You want a copy do this:

unsigned char *p = (unsigned char*)&CurrentPosition;
unsigned char arr[4];
arr[0] = p[0];
arr[1] = p[1];
arr[2] = p[2];
arr[3] = p[3];

and work with arr. (If you want most significant byte first change the order in those assignments).

If You prefer 4 variables You can obviously do:

unsigned char CP1 = p[0];
unsigned char CP2 = p[1];
unsigned char CP3 = p[2];
unsigned char CP4 = p[3];
Maciej Hehl
This soln is better if you can't shift a 32-bit int directly.
Paul Nathan
+2  A: 

I think you should consider using a union:

union {
   unsigned long position;
   unsigned char bytes[4];
} CurrentPosition;

CurrentPosition.position = 7654321;

The bytes can now be accessed as: CurrentPosition.bytes[0], ..., CurrentPosition.bytes[3]

Justin Muller
A good idea in practice because the compiler can do the decomposition of the int into bytes. A bad idea in theory because this usage of unions is undefined by the c-standard.. (most if not all implementations will just work though)
Nils Pipenbrinck
A: 
unsigned char *CP = &CurrentPosition;

Now CPn per your original code is accessed via CP[n].

Matt Joiner