Thread: number representation

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12

    number representation

    Hola! im new to C programming and taking some CS classes at the university. My instructor asked us to write a C program to figure out what num notation is used by your machine... 1's, 2's complement or signed magnitude and I dont even know how to begin. Im not loking for code just ideas on how to do this.

    gracias

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First, review your understanding of one's complement, two's complement, and sign and magnitude. Consider say, -1. Assuming an n-bit integer, what is the representation of -1 in each of these systems? Then, check the interesting bits of -1 to see which it corresponds to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    Quote Originally Posted by laserlight View Post
    First, review your understanding of one's complement, two's complement, and sign and magnitude. Consider say, -1. Assuming an n-bit integer, what is the representation of -1 in each of these systems? Then, check the interesting bits of -1 to see which it corresponds to.
    Hola! i know all 3 but dont know how to do it programaticaly. So por example if 4 bits then -1 will be 1111 in 2's complement and 1110 in 1's complement and 1001 in signed magnitude... correcto?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. What I have in mind with this suggestion is the use of bitwise operators.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Yes. What I have in mind with this suggestion is the use of bitwise operators.
    Do you mean << and >> operators... so how to do it with them.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fsanchez
    Do you mean << and >> operators... so how to do it with them.
    Yes, and also bitwise and. A tutorial on the topic should be enough to get you going, so just search the Web
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Yes, and also bitwise and. A tutorial on the topic should be enough to get you going, so just search the Web
    Muy bien i was able to come up with a program to find no. representation of machine but when i run it on several machines in my lab they all give ans of 2's complement. Now i suppose to explain why and here is code based on your sugestion.
    Code:
    #define NUMERO 0xF
    int main() {
            switch (-1&0xF) {
                    case NUMERO: printf("2's complement\n"); break;
                    case NUMERO-1: printf("1's complement\n"); break;
                    default: printf("signed magnitude\n"); break;
            }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 11:08 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM