i working on endianess. little endian program works, , gives correct output. not able way around big endian. below have far. know have use bit shift , dont think doing job @ it. tried asking ta's , prof not help. have been following link (convert big endian little endian in c [without using provided func]) understand more cannot still make work. thank help.
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { file* input; file* output; input = fopen(argv[1],"r"); output = fopen(argv[2],"w"); int value,value2; int i; int zipcode, population; while(fscanf(input,"%d %d\n",&zipcode, &population)!= eof) { for(i = 0; i<4; i++) { population = ((population >> 4)|(population << 4)); } fwrite(&population, sizeof(int), 1, output); } fclose(input); fclose(output); return 0; }
i'm answering not give answer solve yourself.
first ask this: how many bits in byte? (hint: 8) next, how many bytes in int
? (hint: 4) picture 32-bit integer in memory:
+--------+ 0x|12345678| +--------+
now picture on little-endian machine, byte-wise. this:
+--+--+--+--+ 0x|78|56|34|12| +--+--+--+--+
what shift operations required bytes correct spot?
remember, when use bitwise operator >>
, operating on bits. 1 << 24
integer value 1
converted processor's opposite endianness.
Comments
Post a Comment