Bitwise operations and functions

This is a discussion on Bitwise operations and functions within the C Programming forums, part of the General Programming Boards category; Hi, I'm currently trying to understand bitwise operations more. I am doing an excersice out of K.N. King that wants ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    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?

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok, i can compile cleanly, i forgot to add prototypes for the two new functions. Any help / explanation for the bitwise operators. I dont understand how they work to get the functions to work how they should. thanks

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,282
    You should probably pick better names than high_byte and low_byte in the rotate left and right functions, since they are using integers, not bytes. Also the shift count you are using for "low_byte" in both rotate routines needs to be fixed. You can use trial and error to figure this out.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    333
    The << and >> shift the number. If you want to shift in place, use <<= and >>=, and this may be a bit more intuitive for you.

    The spare bits are simply discarded. You use the & operator to mask off bits, and the | operator to glue parts of the number back together again.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,282
    Quote Originally Posted by Malcolm McLean View Post
    If you want to shift in place, use <<= and >>= ... the spare bits are simply discarded.
    He needs the spare bits since he's trying to implement rotate, <<= or >>= won't help in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operations
    By drkidd22 in forum C Programming
    Replies: 2
    Last Post: 11-12-2009, 10:09 PM
  2. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  3. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2007, 03:01 PM
  4. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-27-2002, 11:46 PM
  5. bitwise operations in C
    By samps005 in forum C Programming
    Replies: 5
    Last Post: 10-10-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21