|
|
View previous topic :: View next topic |
Author |
Message |
otuzsubat
Joined: 24 Jun 2004 Posts: 3
|
long int & pointer |
Posted: Wed Aug 11, 2004 7:32 am |
|
|
unsigned long temp;
byte *ptr;
ptr=&temp;
in this code, does ptr show high byte of "temp"? or low byte? |
|
|
valemike Guest
|
"Little Endian" |
Posted: Wed Aug 11, 2004 10:14 am |
|
|
For want of a descriptive term, it's stored as "Little Endian", i.e. LSByte first.
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
void main(void)
{
unsigned long mike;
unsigned char *byte_p;
mike = 0x1234;
byte_p = &mike;
while(1);
}
|
I simulated it in MPLAB, and found:
address of "mike": 0x06
So the contents of byte_p is 0x06
The contents in RAM:
0x06: 0x34
0x07: 0x12
So if you cast byte_p to mike, you get 0x34.
If you cast byte_p to (mike + 1), you get 0x12.
-Mike
p.s. This is usually encountered in programming quizes during job interviews. They ask you, "How can you tell if the processor is using Big Endian or Little Endian?" The answer is to make an int, assign 0x12345678 to it, then cast a char pointer to the int. If you read back 0x12, then it's BIG endian, otherwise it's Little endian. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|