Thread: Coding Binary

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    Coding Binary

    Good Morning,
    I am working on a project for school that requires me to compute binary numbers and I am having trouble with it. It needs to be able to handle an input of binary, octal, decimal, and hexadecimal. For each input it needs to have the output of the sign/magnitude, one's complement, and two's complement.

    How do I do something like this?

    Thank you very much,
    Mike

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Read input
    Calculate output
    Show output to user

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    How do I calculate the output in C?

    Thanks,
    Mike

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Read input
    For this, I assume that you were given a sample input file/text of some type? Depending on how the data is to be presented, there thousands of ways to read data.

    Provide some example input and post what code you have so far.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    input (in terminal): ./mynumber hex -2D
    output:
    Sign/Mag: 1000000000101101
    1's: 1111111111010010
    2's: 1111111111010011

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    okay, so what you'll want to do is to look at index one of argv (I would toupper or tolower that joker for comparison purposes), do a strncmp against a hard-coded array of predefined keywords (looking for which one it is), then put that value in a switch to handle the processing of index two.

    Now, POST SOME CODE!!!

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Here is what I have so far... I check it to make sure all of the arguments are there, then convert it to binary, then display. The problem I am having is converting everything to binary and then displaying it in either sign/mag, 1's, or 2's.

    Thanks for your help,
    Mike

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
      // Check to make sure all of the arguments are inputted, otherwise print invalidUsage()
      if (argc != 3){
        // Display invalid usage
        invalidUsage();
      } else {
        // Convert the input into binary
        long v = convertInput(argv[1], argv[2]);
        // Print the output of the conversions
        doOutput(v);
      }
      return 0;
    }
    
    int convertInput(char *format, char *value){
      // Choose the type of input
      if(strcmp(format, "bin") == 0){
        // If the input is binary
        convertBinary(value);
      } else if(strcmp(format, "oct") == 0){
        // If the input is octal
        convertOctal(value);
      } else if(strcmp(format, "dec") == 0){
        // If the input is decimal
        convertDecimal(value);
      } else if(strcmp(format, "hex") == 0){
        // If the input is hexadecimal
        convertHex(value);
      } else {
        // If it is none of the above
        invalidUsage();
      }
    }
    
    int invalidUsage(){
      // If the input is incorrect, display mynumber usage
      printf("Usage: ./mynumber [bin binary number]\n\t\t  [oct octal number]\n\t\t  [dec decimal number]\n\t\t  [hex -hexadecimal number]\n");
    }
    
    doOutput(long value){
      // Sign/Magnatude output
      doOutputSignMag(value);
      // One's Complement output
      doOutputOnes(value);
      // Two's complement output
      doOutputTwos(value);
    }
    
    int convertBinary(char *str){
      
    }
    
    int convertOctal(char *str){
    
    }
    
    int convertDecimal(char *str){
    
    }
    
    int convertHex(char *str){
      
    }
    
    doOutputSignMag(long value){
    
    }
    
    doOutputOnes(long value){
    
    }
    
    doOutputTwos(long value){
      
    }

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Okay, so what happens if i put "HeX" as the identifier?

    PUTTING YOU IN THINKING MODE:
    What are you attempting to accomplish? (Why?)

    How would you do this if you didn't have a computer? (That'd be, work out the solution on paper or in your head).

    You'll find that you don't need a function for each type, you'll just need to be smart about the modification of a couple of functions.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Everything is in binary on a computer. What you want to do is this:
    -Read input
    -Calculate one's and two's compliment
    -Display in binary format

    How to calculate one's and two's compliment? That is rather simple if you know how to do it with a pen and paper. Do you know how to do bitwise operations in C?

    How to display in binary format? It would be nice if printf() had this option. But it doesn't. If you google search for this you will find some answers. Generally, check if digit is 0 or 1. If 0 prinft("0"). If 1 printf("1"). Again, you will probably want to use bitwise operators.

    The read input part you already have.

    Don't get confused with the format of data. Everything is binary when it comes to calculations. In C you can use XOR, AND, OR operators on data. So you should have no problem.

    If you are still stuck provide at least some pseudo-code on how to calculate one's or two's complement

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by C_ntua View Post
    Everything is binary when it comes to calculations. In C you can use XOR, AND, OR operators on data. So you should have no problem.
    Not exactly true, when it comes to floating point numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    I read the cprogramming.com bitwise tutorial and it helped me a bit... I hope.
    Below is updated code for what I think I have figured out.
    I have a feeling that in using int I am getting some kind of overflow... which to me also means I am not doing this correctly.
    I am also not sure what to do with negative numbers if they were to appear.

    Thank you very much,
    Mike



    Code:
    int convertBinary(char *str){
      return atoi(str);
    }
    
    doOutputSignMag(int value){
      int i;
      for(i = 15; i >= 0; i--){
        if(value & 1 << i){
          printf("1");
        } else {
          printf("0");
        }
      }
      printf("\n");
    }
    
    doOutputOnes(int value){
      int i;
      for(i = 15; i >= 0; i--){
        if(~value & 1 << i){
          printf("1");
        } else {
          printf("0");
        }
      }
      printf("\n");
    }
    
    doOutputTwos(int value){
      int i;
      value = value + 1;
      for(i = 15; i >= 0; i--){
        if(value & 1 << i){
          printf("1");
        } else {
          printf("0");
        }
      }
      printf("\n");
    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are using signed variables, then yes you'll get some overflow when you hit the sign bit. It also depends on if you have 16bit or 32bit integers (or 64).


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    How can I tell if it is signed bits and if it is 16 or 32bit integers?

    Thanks,
    Mike

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the sizeof operator.
    Code:
    printf( "The size of my integers are %d * %d.\n", sizeof( int ), CHAR_BIT );
    If you use CHAR_BIT, it's in <limits.h>.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    The result from that is 4*8, which is 32.
    The problem I am working on wants 16 bits.

    So, am I on the right track with doing the sign/magnitude, ones complement, and twos complement for doing the output?

    Thanks,
    Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM