2

Following is a piece of C++ code which assigns integer variable address to character pointer.

int ia = (1<<25) + (1<<24) + (1<<17) + (1<<8) + 4;
char *cArr = (char *)(&ia);
int i;
for(i = 0; i < 4; i++){ cout << (int)cArr[i] << endl; }

I have chosen the integer variable so that the MSB byte can have a value of 3, the next value of 2, the next a value of 1 and the LSB byte a value of 4.

Now when I assign the integer's address to a character pointer then I get the following result:

cArr[0] = 4
cArr[1] = 1
cArr[2] = 2
cArr[3] = 3

I have some questions regarding this result:

  1. Is it always that the LSB byte will get the first address irrespective of the machine on which I am working.
  2. Has the result something to do with Little Endian and Big Endian. Also I am looking for some basic tutorial which can get me the basics of Little Endian and Big Endian.
  3. Has the result something to do with how the stack grows on a machine.

By the way I work on a windows PC

3
  • 1
    en.wikipedia.org/wiki/Endianness Commented Dec 15, 2013 at 8:20
  • 1
    Why write this code int ia = (1<<25) + (1<<24) + (1<<17) + (1<<8) + 4; Commented Dec 15, 2013 at 8:27
  • Indeed. It should either be int ia = 0x03020104 or at least int ia = (3 << 24) + (2 << 16) + (1 << 8) + (4 << 0). Commented Dec 15, 2013 at 9:01

2 Answers 2

2

1). No that depends upon the computer architecture and hardware. Here is the table that can be taken as a reference. enter image description here

2). Basic tutorial. Hope this would suffice. http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

3).No nothing to do with stack as you see from table windows follow, Little Endian and hence LSB place first.

let me know if that answers you.

Bye!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! your answer helps. But the table in your answer takes into account operating systems like Windows, Mac or Linux in deciding endian format. But as I know big or little endian format is dependent on the CPU processor architecture. The Operating System (OS) does not factor into the endianess of the system. I am quoting it from this link link
you are absolutely right. Actually this mainly comes from the fact the windows is mostly intel processor and intel use little endian. but if you install windows in SPARC or PowerMac(not sure if this is possible) you will find the other way. Or in other words C program compiled in windows may behave differently in PowerMac machines.
1
  1. No. The results depends on your system hardware type. Only little-endian machines get your results.

  2. Just as question 1, this indeed is determined by little-endian or big-endian. Here is a comprehensive introduction on endian. http://www.yolinux.com/TUTORIALS/Endian-Byte-Order.html

  3. There is nothing to do with stack in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.