PDA

View Full Version : Working with binary




Soulstorm
Dec 16, 2005, 12:42 PM
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?



Mitthrawnuruodo
Dec 16, 2005, 12:58 PM
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
Dec 16, 2005, 12:59 PM
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.

robbieduncan
Dec 16, 2005, 02:29 PM
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 (http://en.wikipedia.org/wiki/Two's_complement)

Soulstorm
Dec 17, 2005, 02:20 AM
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:

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
Dec 17, 2005, 02:40 AM
You need to use single quotes instead of double quotes for characters, ie:
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[i]=="1", you are comparing the ith character of p to the address of a '1' character.

Josh396
Dec 17, 2005, 02:52 AM
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 (http://en.wikipedia.org/wiki/Two's_complement)
Good ol' Two's Complement. That was probably the easiest part about EE.

HiRez
Dec 17, 2005, 03:08 AM
[/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):#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
Dec 17, 2005, 04:18 AM
You need to use single quotes instead of double quotes for characters, ie:
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[i]=="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.

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);
}
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
#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
Dec 17, 2005, 10:19 AM
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
Dec 18, 2005, 09:58 PM
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";
}
}


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.

HiRez
Dec 19, 2005, 12:32 AM
>> 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
Dec 19, 2005, 01:44 AM
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.

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
Feb 9, 2014, 09:40 AM
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?
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
Feb 9, 2014, 09:57 AM
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
Feb 16, 2014, 04:51 AM
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:)