find MSB of unsigned char

This is a discussion on find MSB of unsigned char within the C Programming forums, part of the General Programming Boards category; Doing some practice problems for a class: In this recitation you will be developing some C functions that will help ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    San Antonio, TX
    Posts
    9

    find MSB of unsigned char

    Doing some practice problems for a class:

    In this recitation you will be developing some C functions that will help get you started on the next assignment.
    You will be using the 8-bit floating point format given in Figure 2.34 of the text[Computer Systems 2nd Edition, Bryant and O'Hallaron], with one sign bit, 4 exp bits and 3 frac bits. In the problems below, we will use an unsigned char to hold the value.

    Read the implementation notes before starting.

    1. Write a program that takes a single command line parameter.
    The parameter is a number represented either in decimal or hexadecimal.
    Use strtol with second parameter NULL and base equal to 0 to convert this to a long.
    Print out the value both in decimal and hexadecimal.


    //ok, so my code for the first part is:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    long int strtol(const char *nptr, char **endptr, int base);
    
    int main(int argc, const car *argv[]){
        long x = 0;
        if (argc != 2){
            fprintf(stderr, "Usage: %s <number>\n", argv[0]);
            return (-1);
        }
        x = strtol(argv[1], NULL, 0);
        
        printf("int: %d\thexadecimal: 0x%x\n", (int)x, (int)x);
        return(1);
    }

    2. Write the function:

    int getSign(unsigned char x);

    that returns 1 if the sign bit is 0 and -1 if the sign bit is 1.

    Add a statement to your main program to test this using the command line parameter converted to an unsigned char.



    My question is:

    How do I go about reading the most significant bit of the unsigned char?
    Is there some way to read the bits out or should i just convert it to base 2, or maybe i just have to see if its greater than 127 as a regular long or int?

    if so, then

    Code:
    int getSign(unsigned char x){
        if ((long)x > 127){
            return (-1);
        }
        return (1);
    }

    that should work, no?



    notes state: Most of the functions in Part 1 (except for getValue) can be written with 1, 2, or 3 lines of code in the body of the function. You can create a double value of infinity by dividing 1 or -1 by 0.0, but if the denominator is a constant, the compiler might complain.
    You can create a double value of NaN by calculating 0.0/0.0, but again the denominator should not be a constant.

    sorry if these seem like weird/stupid questions. it's been a while since i used C and i really need to grasp all this computer arithmetic stuff soon... test next monday and it's going to be mostly bit manipulation and etc

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    It's probably better to compare it with a 'mask' that just has the one bit set. You could use:
    Code:
    #define MSB_UCHAR	((unsigned char)(128))    /**< Sets to u_char MSB */
    
    int getSign(unsigned char x){
        if ((long)x & MSB_UCHAR){
            return (-1);
        }
        return (1);
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    San Antonio, TX
    Posts
    9
    oh ok, i forgot about the bitwise operators actually, thanks!

    if the input was larger, would this still work? does bitwise AND add more zeroes to the front of the smaller number or does it just AND the available bits?

    it seems that if the input was larger, it may not AND correctly because the ifs assume an 8 bit number, correct? of course, this shouldn't be an issue since the instructions state that we're taking in an 8-bit number, i was just wondering.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    San Antonio, TX
    Posts
    9
    Quote Originally Posted by Jamdog View Post
    It's probably better to compare it with a 'mask' that just has the one bit set. You could use:
    Code:
    #define MSB_UCHAR	((unsigned char)(128))    /**< Sets to u_char MSB */
    
    int getSign(unsigned char x){
        if ((long)x & MSB_UCHAR){
            return (-1);
        }
        return (1);
    }


    will something similar be required for getExp() and getFrac() functions?
    Write the function:
    int getExp(unsigned char x);
    that returns the exp field of the number as an int. This value should never be negative.
    Add a statement to your main program to test this.

    Write the function:
    int getFrac(unsigned char x);
    that returns the frac field of the number as an int. This value should never be negative.
    Add a statement to your main program to test this.

    Write the function:
    int isZero(unsigned char x);
    that returns true if the parameter is zero and false otherwise.
    Remember that there are two values that give zero.
    Add a statement to your main program to test this.

    for example, i think a right shift 3 spaces will allow me to remove the frac bits of an unsigned char in order to just pass the char as a 4-bit int

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Now for the real challenge:
    Could you write the function so that it worked regardless of the size of a char. I.e. the number of bits in a byte were possibly something other than 8; e.g. 10?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char compile error
    By dallypost in forum C Programming
    Replies: 10
    Last Post: 03-18-2010, 04:54 PM
  2. Memory Allocation Problems
    By amac in forum C Programming
    Replies: 9
    Last Post: 12-10-2009, 06:49 AM
  3. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM

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