1 Attachment(s)
Need help with creating Binary Class
I am trying to create a class to manipulate binary number including +, -, & * functions. here is what i already have.
Code:
#include <iomanip.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
class bin_num{
char data[8];
int todec(bin_num x);
bin_num tobin(int x);
bin_num operator +(bum_num x);
bin_num operator -(bum_num x);
bin_num operator *(bum_num x);}
bin_num::int todec(bin_num x){
int y;
y = strtol(x, NULL, 2);
return y;
}
bin_num::bin_num tobin(int x){
char bnum[8];
itoa(x, bnum, 2);
return bnum;
}
bin_num::bin_num operator +(bum_num x){
}
bin_num::bin_num operator -(bum_num x){
}
bin_num::bin_num operator *(bum_num x){
}
i don't quite know what the operator functions should be. If anyone could help, PLEASE!!!!
Re: to answer your question
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.
Quote:
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 "!=".
a simple sample:
Code:
01100001 (97)
^10110110 (182)
-------------
11010111 (215)
cout << (97 ^ 182);
says "215"
simple sample on xor encryption
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