6. 大尾数法或小尾数法

来源:互联网 发布:云计算网络工程师 编辑:程序博客网 时间:2024/05/19 02:03

    编写一个函数,确定一台计算机采用大尾数法(big-endian)还是小尾数法(little-endian)。大小尾数问题指的是计算机存储多字节值时字节的顺序。

    程序代码如下:

    #include "stdafx.h"

    #include <iostream.h>

 

方案1:

    bool endianness()

    {

        int testNum;

        char *ptr;

 

        testNum = 1;

        ptr = (char *)&testNum;

        return (*ptr);

    }

 

方案2:

    bool endianness()

    {

        union

        {

            int theInteger;

            char singleByte;

        }endianTest;

 

        endianTest.theInteger = 1;

        return endianTest.singleByte;

    }

 

    int main()

    {

        bool a = endianness();

        if(a)

            cout << "大尾数" << endl;

        else

            cout << "小尾数" << endl;

        return 0;

    }

 

    程序的执行结果如下:

 

原创粉丝点击