Analysis and Summary of methods for determining system size

Source: Internet
Author: User

From http://blog.csdn.net/delphiwcdj/article/details/6234383

Problem: How can I use a program to determine the storage mode of the current system (large-end or small-end )? Write a C function. If the processor is big-Endian, 0 is returned. If the processor is little-Endian, 1 is returned.

Case 1: array type

  1. # Include <cstdio>
  2. Int checksystem ()
  3. {
  4. Char s [] = "1000 ";
  5. Return (s [0] = '1 ');
  6. }
  7. Int main ()
  8. {
  9. Checksystem () = 1? Printf ("Little-Endian/N"): printf ("big-Endian/N ");
  10. Return 0;
  11. }

Case 2: Displacement Calculation

  1. Int I = 1;
  2. If (1> 32 = 0)
  3. Cout <"small-end mode" <Endl;
  4. Else
  5. Cout <"big end mode" <Endl;

 

Is the above method correct? Why is it incorrect?

 

This is because the size end is strictly linked to the memory instead of making a comment on the value. If int A = 1; then a & 1 = 1 must be true, because it is calculated from the numerical point of view and has blocked the problem of size. Int A = 1; * (char *) (& A) = 1.

The following describes some effective methods.

Method 1: Use the Union type -- the feature of the Union type data can be used: the starting addresses of all Members are consistent.

  1. # Include <cstdio>
  2. Int checksystem ()
  3. {
  4. Union check
  5. {
  6. Int I;
  7. Char ch;
  8. } C;
  9. C. I = 1;
  10. Return (C. CH = 1 );
  11. }
  12. Int main ()
  13. {
  14. Checksystem () = 1? Printf ("Little-Endian/N"): printf ("big-Endian/N ");
  15. Return 0;
  16. }

 

Method 2: force int type conversion

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. Int main ()
  4. {
  5. Int I = 1;
  6. (* (Char *) & I = 1 )? Printf ("Little-Endian/N"): printf ("big-Endian/N ");
  7. System ("pause ");
  8. Return 0;
  9. }

 

Method 3: Use union and macro to define

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. Static Union
  4. {
  5. Char A [4];
  6. Unsigned long ul;
  7. } Endian = {'l ','? ','? ',' B '}};
  8. # Define endian (char) endian. UL)
  9. Int main ()
  10. {
  11. Printf ("% C/N", endian );
  12. System ("pause ");
  13. Return 0;
  14. }

 

Supplement:
The effect of the size-end mode on Union data.

  1. # Include <cstdio>
  2. Union
  3. {
  4. Int I;
  5. Char A [2];
  6. } * P, U;
  7. Int main ()
  8. {
  9. P = & U;
  10. P-> A [0] = 0x39;
  11. P-> A [1] = 0x38;
  12. Printf ("% x/N", p-> I); // 3839 (Hex .)
  13. Printf ("% d/N", p-> I); // 111000 00111001 14393 = (decimal)
  14. Return 0;
  15. }

 

Shows the analysis:
High address and low address
-- Int
0 | 0 | 56 | 57
--------
-- Char
56 | 57
----
Here we need to consider the storage mode: Big-end mode and small-end mode.
Big-Endian: Low bytes of data are stored in the high address.
Little-Endian: the low bytes of data are stored in the low address.
The space occupied by Union-type data is equal to the space occupied by the largest Member. The access to Union-type members is started at 0 from the offset of the base address of the Union, that is, the access to a consortium starts from the first address of the Union regardless of the variable. Therefore, the output result of the above program is obvious.

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.