Bitwise operations and functions
Hi, I'm currently trying to understand bitwise operations more. I am doing an excersice out of K.N. King that wants me to write functions given the prototypes. rotate_left is suppose to return the result of shifting the bits in i to the left by n places, with the bits that were "shifted off" moved to the right end of i. Example: the call rotate_left(0x12345678, 4) should return 0x23456781, this is if the ints are 32 bits long. Heres what i currently have, I want to see what it does but i get errors when compiling, Any help with that please?
Quote:
gcc -o bitwise bitwise.c
bitwise.c:28:14: error: conflicting types for ‘rotate_left’
bitwise.c:14:42: note: previous implicit declaration of ‘rotate_left’ was here
bitwise.c:36:14: error: conflicting types for ‘rotate_right’
bitwise.c:15:43: note: previous implicit declaration of ‘rotate_right’ was her
Code:
#include <stdio.h>
unsigned short swap_bytes(unsigned short i);
int main(void)
{
unsigned int i;
int j;
printf("Enter a hexadecimal number (up to four digits): ");
scanf("%ux", &i);
printf("Enter a number: ");
scanf("%d", &j);
printf("Number rotated left: %ux\n", rotate_left(i, j));
printf("Number rotated right: %ux\n", rotate_right(i, j));
/*printf("Number with bytes swapped: %hx\n", swap_bytes(i));*/
return 0;
}
unsigned short swap_bytes(unsigned short i)
{
unsigned short high_byte = i << 8;
unsigned short low_byte = i >> 8;
return high_byte | low_byte;
}
unsigned int rotate_left(unsigned int i, int n)
{
unsigned int high_byte = i << n;
unsigned int low_byte = i >> n;
return high_byte | low_byte;
}
unsigned int rotate_right(unsigned int i, int n)
{
unsigned int high_byte = i >> n;
unsigned int low_byte = i << n;
return high_byte | low_byte;
}