how can i find out whether my cpu is low indian or high indian?

This is a discussion on how can i find out whether my cpu is low indian or high indian? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int main() { int x = 0x12345678; char* p = reinterpret_cast<char*>(&x); std::cout << std::hex << "0x" << ...

1. Code:
```#include <iostream>

int main()
{
int x = 0x12345678;
char* p = reinterpret_cast<char*>(&x);
std::cout << std::hex << "0x" << (int)p[0] << "\n0x" << (int)p[1] << "\n0x" << (int)p[2] << "\n0x" << (int)p[3] << std::endl;
if (p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12)
std::cout << "Congratulations! Your machine is little endian!\n";
else if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
std::cout << "Congratulations! Your machine is big endian!\n";
else
std::cout << "Weird. Your machine is neither little endian nor big endian.\n";
}```
Output:
0x78
0x56
0x34
0x12
Congratulations! Your machine is little endian!
Press any key to continue . . .

2. Originally Posted by Elysia
Code:
```#include <iostream>

int main()
{
int x = 0x12345678;
char* p = reinterpret_cast<char*>(&x);
std::cout << std::hex << "0x" << (int)p[0] << "\n0x" << (int)p[1] << "\n0x" << (int)p[2] << "\n0x" << (int)p[3] << std::endl;
if (p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12)
std::cout << "Congratulations! Your machine is little endian!\n";
else if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
std::cout << "Congratulations! Your machine is big endian!\n";
else
std::cout << "Weird. Your machine is neither little endian nor big endian.\n";
}```
Output:
0x78
0x56
0x34
0x12
Congratulations! Your machine is little endian!
Press any key to continue . . .
Thanks a million Elysia .
so basically we should use static_cast <>() after converting the pointer.
so that it would give us the value stored in the new string !
i thought our new character pointer actually contains an address!
so even if i convert that into integer! it is still an address! how does it give me the first value though!
now we use the following command to convert it to a char pointer
Code:
```int const variable = 0x12345;
char const* char_pointer = reinterpret_cast<char const *>(&variable)```
so now our char pointer contains the Address! of the variable !
i think it should be sth like for example these below:
char_pointer[0]=0x00000002;
char_pointer[1]=0x00000004;
char_pointer[1]=0x00000006;
char_pointer[1]=0x00000008;

so in order to get the value stored in Byte we need to go to first location which is stored in char_pointer[0]! but how is it possible without any kind of dereferencing ?
would anyone explain this to me ?
im trying to understand reinterpret_cast<>() stuff!
and the latest part really confused me !
i though we'd need to use reinterpret_cast<> again to be able to see the information we want! but it seems i was wrong.

3. Maybe this will make it more clear:

Code:
```#include <iostream>

int main( void )
{
using namespace
std;
typedef unsigned char
byte;
typedef byte*
pbyte;
typedef void*
pvoid;
size_t
value = 0x12345678;
pbyte
ptr = pbyte( &value ),
seq = ptr;
for( size_t index = 0; index < sizeof( value ); ++index, ++seq )
{
cout << "[" << pvoid( &ptr[ index ] ) << "] " << pvoid( ptr[ index ] ) << endl;
// ...same as...
cout << "[" << pvoid( ptr + index ) << "] " << pvoid( *( ptr + index ) ) << endl;
// ...same as...
cout << "[" << pvoid( seq ) << "] " << pvoid( *seq ) << endl;
}
return 0;
}```
So you see, array indexing *is* dereferencing a pointer.

Also, the reason why the cast from char to void* is necessary is simply because when an istream encounters a char or unsigned char type, it prints it's ASCII value.

4. thank you sebastioni
really tanx
so the c++ way of doing it would be sth like
Code:
```//in the name of GOD
//CPU Analyzer
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::dec;
using std::hex;
int main()
{
size_t value;
char * char_pointer ;

cin>>hex>>value;

char_pointer = reinterpret_cast<char *>(&value);
cout<<"first byte in memory "<<reinterpret_cast<void*>(char_pointer[0]);

return 0;
}```
the catch here is that when dereferencing our char pointer , we need to make the compiler aware that we need the value! so we tell him! not to convert them to ascii!
and we do this by using either (void*) or (int*) in reinterpret_cast<>()
. thats great .
thank you so much guys .

5. Let me explain to you in more details how this works.
Code:
```#include <iostream>

int main()
{
// The memory layout we will be examining.
int x = 0x12345678;
// Convert address to a char* pointer so we can examine byte-by-byte
char* p = reinterpret_cast<char*>(&x);
// p[0] dereferences the pointer and fetches the byte at the current position that the char pointer points to.
// Similarly, p[n] fetches the nth byte from the position of the pointer p.
// Since it is a char* pointer, when dereferencing the pointer, we get a char.
// Naturally, since char is a character, std::cout will print out the character it represents.
// To avoid this, we cast it to int to make sure it prints out the actual byte.
// Coupled with std::hex prints out the hex presentation of the number.
// This is only necessary when printing.
std::cout << std::hex << "0x" << (int)p[0] << "\n0x" << (int)p[1] << "\n0x" << (int)p[2] << "\n0x" << (int)p[3] << std::endl;
// This is the same as above. We dereference the pointer and checks its value.
// Note that we do not need to cast it here. A char contains an integer like anything else, but
// std::cout will interpret it as a character, so that is the reason we must cast in order to avoid that.
if (p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12)
std::cout << "Congratulations! Your machine is little endian!\n";
else if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
std::cout << "Congratulations! Your machine is big endian!\n";
else
std::cout << "Weird. Your machine is neither little endian nor big endian.\n";
}```
Do not cast a byte value into a pointer.

6. Originally Posted by Elysia
Let me explain to you in more details how this works.
Code:
```#include <iostream>

int main()
{
// The memory layout we will be examining.
int x = 0x12345678;
// Convert address to a char* pointer so we can examine byte-by-byte
char* p = reinterpret_cast<char*>(&x);
// p[0] dereferences the pointer and fetches the byte at the current position that the char pointer points to.
// Similarly, p[n] fetches the nth byte from the position of the pointer p.
// Since it is a char* pointer, when dereferencing the pointer, we get a char.
// Naturally, since char is a character, std::cout will print out the character it represents.
// To avoid this, we cast it to int to make sure it prints out the actual byte.
// Coupled with std::hex prints out the hex presentation of the number.
// This is only necessary when printing.
std::cout << std::hex << "0x" << (int)p[0] << "\n0x" << (int)p[1] << "\n0x" << (int)p[2] << "\n0x" << (int)p[3] << std::endl;
// This is the same as above. We dereference the pointer and checks its value.
// Note that we do not need to cast it here. A char contains an integer like anything else, but
// std::cout will interpret it as a character, so that is the reason we must cast in order to avoid that.
if (p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12)
std::cout << "Congratulations! Your machine is little endian!\n";
else if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
std::cout << "Congratulations! Your machine is big endian!\n";
else
std::cout << "Weird. Your machine is neither little endian nor big endian.\n";
}```
Do not cast a byte value into a pointer.
p[0] dereferences the pointer and fetches the byte at the current position that the char pointer points to.
Similarly, p[n] fetches the nth byte from the position of the pointer p.
Since it is a char* pointer, when dereferencing the pointer, we get a char.
Naturally, since char is a character, std::cout will print out the character it represents.
To avoid this, we cast it to int to make sure it prints out the actual byte.
Coupled with std::hex prints out the hex presentation of the number.
This is only necessary when printing.
now its plain as day xD
Thanks alot Elysia , really tanx! now fully understand that stuff .

Page 2 of 2 First 12