12 #include <sys/types.h>
23 #define MAX_NUMS ((INT32_MAX >> 5) + 1)
33 union { uint32_t u;
char c[
sizeof(uint32_t)]; } bint = { 0x01020304 };
35 return bint.c[0] == 4;
45 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
47 return (val << 16) | ((val >> 16) & 0xFFFF);
58 int main(
int argc,
char** argv)
60 const char* usage =
"usage: %s filename\n";
73 assert(
sizeof(
int) >= 4);
79 fprintf(stderr, usage, argv[0]);
82 memset(have_num, 0,
sizeof(have_num));
86 if (0 > (fd = open(argv[1], O_RDONLY)))
94 while(0 < (n = read(fd, buf,
sizeof(buf))))
106 int32_t num = buf[i];
118 uint32_t msk = 1 << (num & 31);
122 have_num[idx] |= msk;
131 fprintf(stderr,
"Total numbers read = %d\n", total_nums);
135 for(
int k = 0; k < 31; k++)
136 if (have_num[i] & (1 << k))
137 printf(
"%d\n", (i << 5) + k);
static bool is_little_endian(void)
Check if the architecture is little endian.
#define BUF_NUMS
read buffer size
int main(int argc, char **argv)
Program to read a file with binary positive 32 bit integers, sort the numbers and print them sorted...
static int32_t reverse_int32(int32_t val)
Reverse the 4 bytes of an int between little and big endian.
NUMS * nums_read(const char *filename)
Read numbers into data structure.
#define MAX_NUMS
Maximum number of 32 bit ints we need to store bits.