Difference between big end machine and small terminal machine

Source: Internet
Author: User
problem

The difference between big-endian and small-end machines.
An in-depth understanding of the computer system contains the following description:

For objects that span multiple bytes, we must establish two principles, what the address of this object is, and how these bytes are arranged in memory. For the first problem, on almost all machines, the multibyte object is stored as a sequential sequence of bytes, so the address of the object is the smallest address in the byte used. For the second problem, also known as the byte order problem. There are two ways of storing it. Big-endian mode: Digital logic high-level storage in the physical low-end mode of memory: Digital logic low storage in the physical low memory

Of course, why should there be this thing?
An in-depth understanding of the computer system gives the following description:

For native programmers, the byte order used by their machines is not visible. However, problems can arise when transferring binary data between different types of machines over the network. Because different types of machines may use different byte sequences, a common problem is that when the data generated by the small-end machine is sent to the big-endian machine, the word in the program word is reversed. To avoid this problem, code writing for a Web application must follow the established rules on the byte order to ensure that the sender machine translates his internal representations into network standards. The receiving party translates the network standard into the internal representation of the receiver. Code Implementation

Primarily for multibyte objects, how to access the contents of a single byte. Consider a consortium in C, accessed using a single-byte object.

#include <stdio.h>

void Check_endian ();

int main (void)
{
    Check_endian ();
    return 0;
}
void Check_endian ()
{
    union{
        char C;//Access first byte
        int data;
    } UN;
    Un.data = 0x01020304;
    if (un.c = = 0x01)
    {
        printf ("Big endian!\n");
    }
    else if (un.c = = 0x04)
    {
        printf ("Little endian!\n");
    }
    else
    {
        printf ("No idea!\n");
    }
}

The following code shows the difference between the host byte order and the network byte order. Intel series CPUs are generally small, the latter being big-endian.

 #include <stdio.h> #include <arpa/inet.h> int main (void) {unsigned short host
    _port = 0x1234;

    unsigned long host_ip = 0x12345678;
    unsigned short net_port = htons (Host_port);

    unsigned long net_ip = htonl (HOST_IP);
    printf ("Host port:% #x \ n", host_port);
    printf ("Host IP:% #lx \ n", host_ip);
    printf ("Net port:% #x \ n", net_port);

    printf ("Net IP:% #lx \ n", net_ip);
return 0; }/* Host port:0x1234 host ip:0x12345678 net port:0x3412//big-endian: logical high-memory physical low-net ip:0x78563412 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.