Thread: Binary

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Binary

    This is not a homework.

    I would like to know an algorithm on how to add and subtract two binary numbers..

    For example, 101 and 111 ( both are binary numbers).

    how am i going to get the result of 111 + 101 and 111 - 101? A simple algorithm is very well appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I would use strtol (or stroul if the numbers are unsigned) to convert the binary strings to integers, and then just add them normally to get the result.

  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    nope.. i would like to implement in the hardway where 101+111 bits are actually carried forward. etc... the typical way of addition.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you want to use two int arrays, and loop through them, in a for loop, and put the answer in another int array?

    Code:
    int i, upper[8] = {1, 0, 1, 0, 0, 0, 1, 1};
    int lower[8] = {0, 1, 0, 1, 1, 0, 1, 0};
    int answer[8];
    
    for(i = 0; i < 8; i++) {
    
      //put your addition code here
    
    }

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    valthyx, what format are your "binary" numbers in to start with? Are they character strings ("111", etc.) or are they held in a C 'char' data type, (0x00000111), or what?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by valthyx View Post
    nope..
    Good luck with that then. You have to convert from strings, otherwise, how can you assign this value to an integer?

    Code:
    int dec = 10, hex = 0xa;
    Looks like directly entering a binary number is just not possible here.

    So, if you don't want to do the string conversion thing, please let us know what new technique you have discovered.

    Of course, Memloop is wrong to say "use strtol()". That will not help. You have to write a function that takes this:
    Code:
    char bin[]="1001000";
    And goes thru it converting each char "bit" to it's appropriate value. Maybe a lookup table will help:
    Code:
    int table_array = { 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
    start at bin[strlen(bin)] and work backwards toward zero (hey -- you probably won't even need that table, you can just keep multiplying your adjustment by 2). Anyway, the prototype for such a function would be:

    Code:
    int binstr2int (char *bin);

    Now you have that binary number as an int and you can do arithmetic with it.

    There is also a goofier way to do this involving a union with bitfields, but it is still conversion of a string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by MK27 View Post
    Of course, Memloop is wrong to say "use strtol()". That will not help.
    Uhm, strtol is a perfectly good solution depending on what answer he desires.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Memloop View Post
    Uhm, strtol is a perfectly good solution depending on what answer he desires.
    Okay, that's true, you can do this:

    Code:
    long int value = strtol("1000100", NULL, 2);
    which converts to base 2 (binary).

    Sorry!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Memloop View Post
    Uhm, strtol is a perfectly good solution depending on what answer he desires.
    Yeah, but then you said to add them together normally, which makes no sense, since it wouldn't be binary anymore.

    111 + 101 = 212

    instead it should be: 111 + 101 = 1100
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cpjust View Post
    Yeah, but then you said to add them together normally, which makes no sense, since it wouldn't be binary anymore.
    They would be binary, that is how ints are stored. strtol() has a parameter base to indicate whether the string is a represention of a hex or decimal or binary number.

    But there is no reverse strtol()!

    So: you could use Memloop's method to encode and mine to decode.
    Last edited by MK27; 01-15-2010 at 07:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    1337
    Join Date
    Jul 2008
    Posts
    135
    I have solved the problem with addition. But i have a problem with subtraction. i have no problem when i use 111 - 100. But the problem occurs when i used 100 - 111. It gives me a wrong answer. Please help. I tried to convert the binary to decimal first then minus.. it didnt work too.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Post your code.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    They would be binary, that is how ints are stored.
    Binary is made of 0's & 1's, not 0's, 1's & 2's. That would be Trinary (or something).

    Edit: Nevermind. I think I mixed up the 2nd & 3rd posts... So yeah, if you convert "111" & "101" to 7 & 5, add them together and then convert the result back to a binary string, that would work. I was thinking of converting them to the numbers 111 & 101, which wouldn't work.
    Last edited by cpjust; 01-15-2010 at 08:51 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by MK27
    Looks like directly entering a binary number is just not possible here.
    And it is with dec or hex? No, it's just done for you.

    Try getting input char by char. This is the most direct way I know of, it relies on the fact that ascii '0' and '1' are sequential. Thus '0'(0x30) - '0'(0x30) actually equals 0 and '1'(0x31) - '0'(0x30) equals 1.
    Code:
    unsigned int bit;
    unsigned int total;
    do{
         bit = getchar() - '0'; // Subtract value of '0', making '0'=0 and '1'=1
         if (bit > 1)
         {
              // handle invalid input
         }
         total *= 2; // Adjust for next bit
         total += bit
         }while (bit != (0xd-'0') && bit != (0xa-'0')); // Check for enter(Idk if getchar() returns CR or LF, so I checked for both)
    I'm sure you can condense it, it's just easier to understand this way.
    Do that twice, the add the two totals.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    If you want to do it the hard way, why not work entirely with strings? You don't have to convert your "numbers" to integers. You could use the binary addition and subtraction rules; for instance, addition: 0+0=0, 0+1=1, 1+0=1, 1+1=0 plus carry. Just step through your strings a character at a time (the characters being '0' and '1'), right to left, and apply the rules to build the sum -- as a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 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