I find some things in your code strange (except that you mispelled bin_num in all arguments to the operators).
todec
why does this take an argument? Do you want to convert any bin_num to an int? If so, make it static. If not, don't take an argument and return the int value of the string in you class. I wouldn't do that as a function, but rather as a cast operator:
Code:
operator int ();
This doesn't take an argument, nor has it an explicit return type. The return type is defined by the type it casts to.
tobin
same thing: either static, or assign value to object. You can return the value if you want (but NOT a local array as you did!) You also maybe want to rename the function to
bin_num(int i);
without return type. This is called a cast constructor and enables you to write this:
Code:
int i = 34;
bin_num bn = (bin_num)i;
In addition, you could write an operator char*, that returns the string associated with the object.
Don't forget the public keyword, at least for the functions (it's not a bad thing to hide the string)
I think an int can be up to about 16 billions (or was it even more?), so you'll need a longer string. (10 maybe?)
The operators:
Basically you have to convert both operands to ints, perform the operation and convert the result back to a bin_num.
For example:
Note that this was the += operator, not the + operator, which is usually a global function:
Code:
bin_num operator +(bin_num & num1, bin_num & num2)
{
bin_num temp; // the constructor should set the int-value to 0
temp += num1;
temp += num2;
return temp;
}
If data is private (should be), you have to declare this function a friend of the class.
You can also write + operators for types like int and char*. You may even want to make it UNICODE compatible by using TCHAR and the _tcs function family and _ttoi or __itot.
One more thing:
Make sure you declare inline functions in the header file of the class (or a subinclude) and normal functions in the class module.
If you don't know any of the mentioned techniques, ask me.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Oh sorry, i think i might've mislead you somehow... you have some mistakes in ur code such as bum_num and all of ur class definition syntax seems to be messed up.
It is supposed to be:
Code:
<return type> <scope (usually classname followed by two colons ::)><function name> ...
Going back to the problem, instead of having tobin and todec functions, it would be best that you overload the following casts:
(int)
also, make the length of data to be 64, because if you are going to convert any int to bin, then you need to be prepared.
Anyways, here is some code:
Code:
// Here are a few more additions to what CornBee started.
void bin_num::operator -=(const bin_num &num)
{
int iThis, iNum, iResult;
iThis = atoi(data);
iNum = atoi((char*)num); // requires cast operator
iResult = iThis - iNum;
itoa(data, iResult);
}
bin_num operator -(bin_num & num1, bin_num & num2)
{
bin_num temp;
temp += num1; // first add the first number and then subtract that from the second
// like this: num1 - num2
temp -= num2;
return temp;
}
void bin_num::operator *=(const bin_num &num)
{
int iThis, iNum, iResult;
iThis = atoi(data);
iNum = atoi((char*)num); // requires cast operator
iResult = iThis * iNum;
itoa(data, iResult);
}
bin_num operator *(bin_num & num1, bin_num & num2)
{
bin_num temp;
temp += num1; // first add the first number and then multiply that from the second
// like this: num1 * num2
temp *= num2;
return temp;
}
// and here's an overload of char*
char* bin_num::operator char*(bin_num& num)
{
return &num.data; // return a pointer to the internal data (which is already a char)
}
And thats all for today, maybe I can write up more for u later.
in having a binary number stored as a string when binary is the format that data will end up in your memory anyway?
a regular char would be much much more efficient.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
if you are making an encryption program, you have to be able to manip the binary code to change the numbers.
ex: (using ASCII vaules)
this is the method used by the CIA
to encrypt the string "abc" first you have to convert it to binary (assuming you don't already have it in binary)
Code:
a b c
01100001 01100010 01100011
then you have to add the secrete code. in this case it is 101. assuming 0 + 0 = 0, 1 + 0 = 1, and 1 + 1 = 0 (not 10)
Code:
a b c
01100001 01100010 01100011 and then coverting
10110110 11011011 01101101 them back to ASCII
-------------------------- \/\/\/\/\/\/\/\/\/
11010111 10111001 00001110 |
hidden hidden hidden <-------+
it returns 3 hidden chars that are only visible as little blocks or blank space to normal people.
that is why storing binary numbers this way is
more efficient than a single char
First off, all integers are stored binary, and processed binary as well, by the cpu. So any number represent a binary value and any operations performed on it is done binary. Naturally this is also the both most performance and memory efficient way.
then you have to add the secrete code. in this case it is 101. assuming 0 + 0 = 0, 1 + 0 = 1, and 1 + 1 = 0 (not 10)
You should use bitwise exclusive or (^) operator on integers. It returns bitwise inequality "!=".
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
most efficient would be to xor 32 bit integers, that means 32 bits in a single cpu instruction, not several per bit on a string
PHP Code:
char a[25]="This is a test string!!";
int key =12345678;
for (int* b=(int*)a;b<(int*)(a+sizeof(a));*b++^=key);
cout << a << endl;
for (b=(int*)a;b<(int*)(a+sizeof(a));*b++^=key);
cout << a << endl;
What i do here is take a char string and xor it against a 32 bit key "12345678". Then xor it back. If *b++^=key looks confusing it is the same as
*b^=key; // xor assign key on itself
b++; //goto next integer
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Humm, somehow I was wondering why anyone would want to convert an int into a bin when they are essentially the same, but when you're trying to view the number in binary, i think there was a built in C/C++ operand that outputs and.or displays binary representation of a number.
Originally posted by MoMad Humm, somehow I was wondering why anyone would want to convert an int into a bin when they are essentially the same, but when you're trying to view the number in binary, i think there was a built in C/C++ operand that outputs and.or displays binary representation of a number.
You mean _itoa? it's a function... what's a bin btw, there's no bin datatype in C++
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Originally posted by MoMad sorry bin = binary number,
and why do u use the functions with underscore? arent they supposedly to be private functions?
itoa doesn't work for me, I need to use the underscore, I have no idea why.
I don't think issue is to view the binary number
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
That could be a reason you HAVE to use the function with underscores.... those functions are ANSI-C. So I have no idea why they dont work for you... try using #include <stdlib.h> and see if it works without the _, and see which is faster, etc
Okay that seems to work, although i noticed that if i have <iostream> instead of <iostream.h> then i can use itoa as well, thats odd
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
aww, u should've told me u had it WITHOUT the 'h'. If you dont have the .h, then you are using the NAMESPACE!! it is very different than using the header that is why.
You could've used it like:
PHP Code:
#include <iostream>
using namespace STD; // i forgot the exact syntax
// now you can call the ito blah blah whatever.
itoa(x,r); // compiles
I think... it was something like that... anyways, its getting late and im just gonna stop debuggin for now and call it a night.
well, for me, it works without using using namespace std as well
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.