11 #include <sys/types.h>
22 #define MAX_NUMS ((INT32_MAX >> 5) + 1)
32 union { uint32_t u;
char c[
sizeof(uint32_t)]; } bint = { 0x01020304 };
34 return bint.c[0] == 4;
44 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
46 return (val << 16) | ((val >> 16) & 0xFFFF);
55 const unsigned int max_nums)
62 assert(NULL != filename);
63 assert(0 < strlen(filename));
64 assert(NULL != have_num);
69 if (0 > (fd = open(filename, O_RDONLY)))
77 while(0 < (n = read(fd, buf,
sizeof(buf))))
101 uint32_t msk = 1 << (num & 31);
103 assert(idx >= 0 && idx < max_nums);
105 have_num[idx] |= msk;
128 const unsigned int max_nums)
131 assert(NULL != have_num);
133 for(
int i = 0; i < max_nums; i++)
135 for(
int k = 0; k < 31; k++)
136 if (have_num[i] & (1 << k))
137 fprintf(fp,
"%d\n", (i << 5) + k);
148 int main(
int argc,
char** argv)
150 const char* usage =
"usage: %s filename\n";
161 fprintf(stderr, usage, argv[0]);
164 if (NULL == (have_num = calloc(
MAX_NUMS,
sizeof(*have_num))))
166 fprintf(stderr,
"calloc of size %lu failed\n",
MAX_NUMS *
sizeof(*have_num));
173 fprintf(stderr,
"Total numbers read = %d\n", total_nums);
static int32_t reverse_int32(int32_t val)
Reverse the 4 bytes of an int between little and big endian.
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 bool is_little_endian(void)
Check if the architecture is little endian.
static void output_numbers(FILE *fp, uint32_t *have_num, const unsigned int max_nums)
Read through the marked bits and print out the numbers in ascending order.
static int store_numbers(const char *filename, uint32_t *have_num, const unsigned int max_nums)
Read numbers from file and set bits in provided array.
#define MAX_NUMS
Maximum number of 32 bit ints we need to store bits.
NUMS * nums_read(const char *filename)
Read numbers into data structure.
#define BUF_NUMS
read buffer size