Thread: counting in binary

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    counting in binary

    Hey everyone another stupid question.
    Is ther a function that allows me to count in binary...cause i been racking my brain for about 2 hours and i cant seem to get the logic rite.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What do you mean with counting in binary?

    0 = 0
    1 = 1
    2 = 10
    3 = 11
    4 = 100
    5 = 101
    etc.

    1 = 2^0 = 1
    100 = 2^2 = 4
    101 = 2^0 + 2^2 = 5

  3. #3
    here's how binary works,

    00 = 0
    01 = 1
    10 = 2
    11 = 3

    we add from right-to-left, if we have 00 we add by 1 which makes 01, and if we add another 1 we cannot go past a 0 or 1 value so we shift the 1 to the left which makes it 10.

    You can tell how many possible combinations in a binary set, such as 1 byte can hold by doing POWERS.

    1 byte is 8 bits.
    0000 0000

    2^8 (2 to the 8th power) = 256 so the values are between 0 - 255 or if signed the values are -127 - 128.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Question

    when i say counting in binary, i just can't figure how to apply it to c...i know how to count in binary but when i apply it to c like 101 + 110 will come out 212 insted of 1011...so i was wondering if ther is a function or a theory on how i can make it so that when i add it adds in binary

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In C++ if you calculate 101 + 110 you get 212, that is correct. It would be incorrect if it were 1011, because C++ uses base 10 (decimal), not base 2 (binary).

    To calculate 101 + 110 in binary, you would first have to convert this from binary to decimal and then calculate the sum. Such like:

    int sum;
    sum = to_decimal (101) + to_decimal (110);

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    What do you mean by apply it to c? Do you mean the language C?

    I don't think there is a function that adds binary like the way you want to, but its easy enough to create one yourself. One way would be to convert the number to decimal, add then, then convert back to binary.

    To do it arithmetic wise, do like in the old days of math, put the numbers on top of each other, and add from right to left carrying over.
    Code:
     101
    +111
    -------
    First you add the right most, and in binary 1+1=10, so carry the one. 1+0+1=10 so carry the one. 1+1+1=11 so the first two digits are 11. The final answer is 1100. So you need to write a function that does this. If you need help writing something like this show us what you got and we'll help out.

  7. #7
    <iomanip.h>

    has functions for printing out Hex and Decimal formats - not sure - but maybe it also has a binary format function?
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    In C and C++ you can only directly express literals in decimal, octal, and hex (and ascii). Not binary.

    It's stored as binary and calculations are in binary because that's the only thing the computer can understand.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    EVERYTHING in the computer is stored in BINARY! So, if you are adding 3+3, the computer is adding 11 (binary) to 11 (binary) and getting 110 (binary). So somewhere inside the computer's RAM and/or CPU register, you would find a memory location where the three least-significant bits are 110.

    The compiler and operating system are “kind-enough” to convert all these numbers back to decimal for you. And, I was surprised to learn recently that you can’t include binary in your source code as you can hex. (You can write decimal 10 as “10” or as hex “0x0A”)

    So, the key point here is that any conversion ONLY happens during input or output.

    I don’t know any more than OneStiffRod about the output conversion. I sort-of know of a function to convert a string in base-x to an integer [similar to atoi() or atoh() ]. But I don’t have an example at-hand.

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You can also express ten as 012

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's one way. This example prints the bits of a signed integer in reverse...I'll leave it to you to make improvements that may be necessary...

    Code:
    int
     set_bit(int * data, int bit)
    {
     return *data |= 1 << bit;
    }
    int
     clear_bit(int * data, int bit)
    {
     return *data &= ~(1 << bit);
    }
    bool
     is_set(int data, int bit)
    {
     return data & (1 << bit);
    }
    char *
     binary(char buff[33], int x)
    {
     buff[32] = 0;
        for(int y = 0; y < 32; ++y )
       {
        buff[y] = is_set( x, y ) ? '1' : '0';
       }
     return buff;
    }
    
    int
     integer(char buff[33])
    {
     int value = 0;
        for(int y = 0; y < 32; ++y )
       {
           if(buff[y] == '1')
          {
           set_bit(&value, y);
          }
       }
     return value;
    }
    
    
    int
     main(){
    char buff[33];
    char input[50];
    int byte4, bit;
    
     while(byte4 != 0){
     cout << "Enter a value" << endl;
     byte4 = atoi(fgets(input, 50, stdin));
     cout << binary(buff, byte4) << " = ";
     cout << integer(buff) << endl;
     cout << "Enter a bit to set (1-32)" << endl;
     bit = atoi(fgets(input, 50, stdin));
        if(bit && bit < 33)
       {
        --bit;
        cout << binary(buff, set_bit(&byte4, bit) ) << " = ";
        cout << integer(buff) << endl;
       } else cout << "Out of range..." << endl;
     cout << "Enter a bit to clear (1-32)" << endl;
     bit = atoi(fgets(input, 50, stdin));
        if(bit && bit < 33)
       {
        --bit;
        cout << binary(buff, clear_bit(&byte4, bit) ) << " = ";
        cout << integer(buff) << endl;
       } else cout << "Out of range..." << endl;
     }
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here are some functions that appear to handle any numeric base:

    For input:
    strtoul() ANSI (string to long)
    strtod() ANSI (string to double)

    For output:
    ultoa() NON-ANSI Microsoft (unsigned long to string)

  13. #13
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Here are some functions that appear to handle any numeric base:
    Up to 36 of course. :-)
    *Cela*

  14. #14
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I'd create a class to deal with binary numbers. Such a class could contain utility functions to convert integers to binary and vice versa. The binary representation of the number could be stored as a string and an int variable could be used for calculations. But, such a class could be somewhat time consuming (at least for me), so I'd only suggest this if you're really going to be working with binary numbers for a considerable amount of time. Just a suggestion.


    Hmmm... Ya know what? I think coding a binary class would be fun. Maybe that'll be my next project. Well, if you see questions in the near future about a binary class, don't be suprised.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. counting 1's in a binary byte
    By muran_pling in forum C++ Programming
    Replies: 23
    Last Post: 08-24-2006, 05:45 PM
  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