Thread: Help with C assignment

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    Help with C assignment

    Hello all,

    I'm trying to do an assignment, but I'm really struggling (in the middle of class someone yelled "THIS IS SO HARD" so I'm pretty sure I'm not the only one haha), and I would appreciate help. To start off, this is the first problem:

    Write a getBits function which takes as input a starting bit number low (int), an ending bit number high (int) and an unsigned int source and extracts from source the bits in the range specified and returns them in the low order bits of an unsigned int. Bit numbering goes from 0 (least significant bit) to 31. If the values of low or high are invalid, the function should return 0. For example:
    printf("%d\n", getBits(2, 5, 6));
    should cause the value 1 to be displayed.

    printf("%d\n", getBits(2, 3, 13));
    should cause the value 3 to be displayed.
    Hint: you can do this with two shifts.

    The next few are pretty similar to this one, so I think if someone can point me in the right direction, I can figure out the rest.

    So far I have :
    Code:
    unsigned int getBits(int low, int high, unsigned int source)
    {
         if (low > high || low < 0 || high > 31)
         {
              return 0;
         }
         unsigned int input = source;
         int mask = ???
    }
    That basically just covers all of the cases for invalid inputs. I've tried a few things with shifting, but I'm not getting anywhere and I think I'm just confusing myself more. We were never really taught about masks, but I read about them in our book, so I'm pretty sure that's what we're supposed to use, but I'm not really clear on how to use them. I feel like I'm supposed to take the source and shift over the high input and store it in a variable, do the same thing with the low, and then do something comparing the two variables but everything I have tried so far hasn't worked. Am I on the right track or totally off?

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    There are more then one way to accomplish this.
    Your teacher are right when he say 'you can do this with 2 shifts'.

    First of all, i would use 'uint32_t' as type, because the normal 'int' type can be greater then 4 byte (or less).
    The type 'uint32_t' is an unsigned integer and fixed to 32 bits (4 byte). It is defined in stdint.h, so make sure you include this header.

    Now a take your first example 'getBits(2, 5, 6)':
    6 = 00000000 00000000 00000000 00000110

    We want only the bits from 2 up to 5:
    00000000 00000000 00000000 00000110

    Can you see haw many bits you must shift?
    The first shift goes 26 positions to the left side:
    00011000 00000000 00000000 00000000

    The blue part are the bits that was shifted in and this bits are allways 0.
    Now comes the second shift. 28 positions to the right side:
    00000000 00000000 00000000 00000001

    And thats the result!
    The same for the second example getBits(2, 3, 13) (in short):
    00000000 00000000 00000000 00001101
    11010000 00000000 00000000 00000000
    00000000 00000000 00000000 00000011

    You must only calculate how wide are the shifts.
    You have 32 bits and low and high to calculate it.
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    2
    Thank you so much! That really cleared things up for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please i need help for my assignment
    By abo5aleil in forum C Programming
    Replies: 10
    Last Post: 04-22-2013, 06:31 PM
  2. Help!! Assignment!
    By Joeshua Lee in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2011, 09:30 AM
  3. Help!! Assignment!
    By Joeshua Lee in forum C Programming
    Replies: 3
    Last Post: 08-09-2011, 09:30 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. C++ Assignment Help Please!
    By oceanicblack in forum C++ Programming
    Replies: 25
    Last Post: 10-18-2008, 03:39 PM