zhangmeng
2024-01-18 8fc23a3bb9f49e88478a2505fa7dee434ec50c16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * is-little-endian.c
 */
 
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <byteswap.h>
 
typedef unsigned char* byte_pointer;
 
int is_little_endian() {
  int test_num = 0xff;
  byte_pointer byte_start = (byte_pointer) &test_num;
 
  if (byte_start[0] == 0xff) {
    return 1;
  }
  return 0;
}
 
 
 int
 main(int argc, char *argv[])
 {
   uint64_t x;
 
   if (argc != 2) {
       fprintf(stderr, "Usage: %s <num>\n", argv[0]);
       exit(EXIT_FAILURE);
   }
 
   x = strtoul(argv[1], NULL, 0);
   printf("0x%" PRIx64 " ==> 0x%" PRIx64 "\n", x, bswap_64(x));
 
   exit(EXIT_SUCCESS);
 }