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. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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 . . .
    Last edited by Elysia; 10-31-2009 at 01:36 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Elysia View Post
    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!
    suppose we had an address of 0X00000002
    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.

    thank you all in advance
    Last edited by Masterx; 10-31-2009 at 08:06 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    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 ;
    
        cout<<"Please Enter your number (in hex)";
        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 .
    Last edited by Masterx; 11-01-2009 at 03:19 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Elysia View Post
    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 .
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 04:41 PM
  2. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 10:46 AM
  3. high or low !
    By Beginner2002 in forum C Programming
    Replies: 3
    Last Post: 07-29-2002, 01:24 PM
  4. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21