Working with binary

Discussion in 'Mac Programming' started by Soulstorm, Dec 16, 2005.

  1. macrumors 68000


    I have written a little program in c++ to show the binary equivalent of a number:
    void dispbinary(unsigned u){
    	int t;
    	for(t=128; t>0; t=t/2){
    		if(u & t)	cout << "1";
    		else		cout << "0";
    But now I want to do the opposite: I want to give a number in binary code and see it normally. for example I want to give "00001010" and receive "10" as a result. how do I do this?

    I tried working with arrays (giving the number in an array) and then working with the array positions one by one to see if the number in place is "0" or "1". But I received an error saying that ISO C++ forbids comparison between pointers and integers...

    What can I do?
  2. Moderator


    Staff Member

    If you turn the binary number around yoo get 01010000. Then it shouldn't be too hard to calculate with a loop:

    Your number down here

    0 * 2^0 = 0
    1 * 2^1 = 2
    0 * 2^2 = 0
    1 * 2^3 = 8
    0 * 2^4 = 0

    2 + 8 = 10

  3. macrumors 601


    Well one way to do it would be to receive the number as a string (array of chars), walk each character of the array from the right side and add up the numbers. Keep a variable that doubles each time through the loop that is the number you will add onto the result if you find a "1" in that position.
  4. Moderator


    Staff Member

    I assume from the above you are just treating all binary input as positive? If you want to be able to have negative numbers you might need to do some reading!

    Two's Complement
  5. macrumors 68000


    that was my idea too, but I encounter a problem:

    I can't compare a pointer and an integer! Look:

    The compiler gets me an error saying "ISO C++ forbids comparison between pointers and integers". Why? How can I bypass that?
  6. Administrator


    Staff Member

    You need to use single quotes instead of double quotes for characters, ie:
    The reason you get that error is that strings are a pointer to their first character, while a character is essentially just an integer. So when you use the code p=="1", you are comparing the ith character of p to the address of a '1' character.
  7. macrumors 65816


    Good ol' Two's Complement. That was probably the easiest part about EE.
  8. macrumors 601


    You need to dereference the pointer to get the value of what it points to (*p instead of p):
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    int main (int argc, const char * argv[]) {
        char *s = "10001001"; // test binary string
        char *p = s;
        int x = strlen(s) - 1; // exponent
        int result = 0;
        while (*p != '\0') {
            if (*p == '1') {
                result += pow(2, x);
        printf("binary %s in base 10 = %d", s, result);
        return 0;
    There are multiple ways to skin this cat, this is just one.
  9. macrumors 68000



    Thanks. That cleared things up a bit, and I can run the program nicely.

    Thanks for taking the time to write/find the program to help me. I appreciate that, you helped me a lot.\

    EDIT: And this is what I intended to do from the start
    #include <math.h>
    #include <iostream>
    using namespace std;
    int main (int argc, const char * argv[]) {
    	int i, sum=0;
        char p[9]= "11011011";
    	int exponent = 0;
    	for(i=7; i>=0; i--){
    			cout << sum << "+2^" << exponent << "="; 
    			sum=sum + (pow(2,exponent));
    			cout <<sum << "\n";
    	cout << "Final result: " << p << " equals " << sum;
    	return 0;
  10. macrumors member

    I found this pretty clever. I'm just getting into bit maniputlaion, which is probably why. Did you come up with that yourself? I mean the idea where all the bits are zero in the right operand, except for the bit number you want to know about in the left operand through anding. (The dividing by two thing is cool too.)
  11. macrumors 6502a


    Just a tip since you are learning about bit manipulation now. Whenever you divide by 2^x (2 to the x-th power), use t >>= x instead of t/pow(2,x), or in this case, do
    t >>= 1
    in place of
    t /= 2
    >> just means a right shift of the bits, and similarly you can use t << x to multiply t by 2^x. If you think about it, they are equivalent, but as you can imagine, shifting bits is much simpler, and thus faster, than actually dividing.
  12. macrumors 601


    True, although I think most modern compilers will automatically optimize division by a power of two to a bit shift, won't they?
  13. macrumors 68000


    Maybe certain compilers won't do that in C++. Since C++ leaves you with the ability to do anything in your system, it must also leave you with the choice to optimize the code yourself.

    Yes, but this program will work for numbers that can be represented with 8 binary digits. If you want more, you simple multiply the "128" with 2 (this will give you 9 digits). If you want 10 digits, give "512", and so on...
  14. macrumors newbie

    sorry for distrub
    iam abeginner and i come to betwise operator and i m googling about this loop : can some one expline how its work this for lo:(op thank you
  15. macrumors 68040

    The variable t is a bitmask and it's initial value of 128 is 10000000. In each iteration t is divided by 2 which have the effect of shifting the 1 to the right, i.e 01000000, 00100000, 00010000 and so on. In the body the mask is then tested by using a bitwise 'and' with the original value 'u', the test is 'true' if the same bit is set in u and t.
  16. macrumors newbie

    thank you very much:)

Share This Page