1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to create a function that tests bits in a byte, and it returns

Discussion in 'C' started by new4c, Jan 4, 2008.

  1. new4c

    new4c New Member

    Joined:
    Jan 3, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,
    I just started taking the C programming class and all of a sudden we getting bombarded with a bit harder questions. So I needed your assistance. I don't know to begin here. I learned about bit shifting, bit setting/resetting and the likes. But I know how to implement it using a function here. Thanks DJ

    Devise a function to test bits in a byte, and it returns
    a) success or failure,
    b) count of bits that are set (“==â€) in the byte,
    c) position of bit(s) in the byte.
     
  2. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    East Coast USA
    A function can only return 1 value. So how can one function "return" those multiple values?
    You could call it a bunch of times with different values... (yuk)
    or You could call it once with something like this:
    Code:
    int function(int *numcount, int *b1, int *b2, int *b3, int *b4, int *b5, int *b6, int *b7, int *b8)
    {
    - - do the stuff to find the desired values 
    - - ASSIGN those values to the variabe addresses, ...(and NOT actually RETURN them)
    - - return a value for success or falure say (1 or 0)
    }
    
    Since those values are all integers you could use an array instead:
    Code:
    #include <stdio.h>
    int main()
    {
      unsigned int sucess, bitinfo[9] = {0};
      
      success = function( bitinfo, sizeof(bitinfo) );
    . . .
    int function( unsigned int *bitinfo , unsigned int size_of_bitinfo)
    
    Alternatively, bitinfo[] could be defined as chars for a smaller footprint since the values will be fairly small!
    You could also use a structure.
    Make any sense?
     
  3. new4c

    new4c New Member

    Joined:
    Jan 3, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks a lot HowardL. I appreciate for you help. As newbie a make sense to some extent I don't if there is a simpler way you can put it. But aleast you gave me a starting point.
     
  4. new4c

    new4c New Member

    Joined:
    Jan 3, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    HOWARDL, can you help me with one too. Thanks a lot again
    a function to toggle the case of string argument, and returns success/failure, the toggled content, and the number of conversions.
    Hint: If a char is in lower-case convert to upper-case, or vice versa.
     
  5. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    East Coast USA
    I'm not sure exactly what you are asking.
    Why don't you post what you have written so far? (enclosed in code tags)
    It will give me a better idea of what level your thinking is.
    I can use it as a reference to make suggestions.
    OK?
     

Share This Page