Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I have written a little program in c++ to show the binary equivalent of a number:
Code:
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?
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,424
1,065
Bergen, Norway
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
|
v

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

2 + 8 = 10

:)
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Soulstorm said:
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?
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.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
HiRez said:
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.
that was my idea too, but I encounter a problem:

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

Code:
if(p[i]=="1"){
code......
}
The compiler gets me an error saying "ISO C++ forbids comparison between pointers and integers". Why? How can I bypass that?
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
You need to use single quotes instead of double quotes for characters, ie:
Code:
if(p[i]=='1'){
code......
}

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.
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Soulstorm said:
[/code]
The compiler gets me an error saying "ISO C++ forbids comparison between pointers and integers". Why? How can I bypass that?
You need to dereference the pointer to get the value of what it points to (*p instead of p):
Code:
#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);
        }
        p++;
        x--;
    }

    printf("binary %s in base 10 = %d", s, result);

    return 0;
}
There are multiple ways to skin this cat, this is just one.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
HexMonkey said:
You need to use single quotes instead of double quotes for characters, ie:
Code:
if(p[i]=='1'){
code......
}

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.

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

HiRez said:
You need to dereference the pointer to get the value of what it points to (*p instead of p):
Code:
#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);
        }
        p++;
        x--;
    }

    printf("binary %s in base 10 = %d", s, result);

    return 0;
}
There are multiple ways to skin this cat, this is just one.
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
Code:
#include <math.h>
#include <iostream>
using namespace std;


int main (int argc, const char * argv[]) {
	int i, sum=0;
    char p[9]= "11011011";
	i=0;
	int exponent = 0;
	for(i=7; i>=0; i--){
		if(p[i]=='1'){
			cout << sum << "+2^" << exponent << "="; 
			sum=sum + (pow(2,exponent));
			cout <<sum << "\n";
		}
		exponent++;
	}
	cout << "Final result: " << p << " equals " << sum;
			
	return 0;
}
 

Jordan72

macrumors member
Nov 23, 2005
88
0
Soulstorm said:
Code:
void dispbinary(unsigned u){
        int t;
        for(t=128; t>0; t=t/2){
                if(u & t)       cout << "1";
                else            cout << "0";
        }
}

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.)
 

NewbieNerd

macrumors 6502a
Sep 22, 2005
512
0
Chicago, IL
Soulstorm said:
I have written a little program in c++ to show the binary equivalent of a number:
Code:
void dispbinary(unsigned u){
	int t;
	for(t=128; t>0; t=t/2){
		if(u & t)	cout << "1";
		else		cout << "0";
	}
}

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
Code:
t >>= 1
in place of
Code:
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
NewbieNerd said:
>> 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.
True, although I think most modern compilers will automatically optimize division by a power of two to a bit shift, won't they?
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
HiRez said:
True, although I think most modern compilers will automatically optimize division by a power of two to a bit shift, won't they?
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.

Jordan72 said:
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.)
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...
 

jakyshan

macrumors newbie
Feb 9, 2014
2
0
I have written a little program in c++ to show the binary equivalent of a number:
Code:
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?
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
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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

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.
 

jakyshan

macrumors newbie
Feb 9, 2014
2
0
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.

thank you very much:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.