printing one and or more complement from binary

closed account (STR9GNh0)
getting from numbers to binary - i think everyone knows.

anyone could help me with binary to achieve one or two or more complement?

any idea what i should use to achieve it ? thanks

1
2
3
4
5
for(loop to get binary)
{
int remain = number % base	
number= number / base;
}


how can i work my way out to achieve complement? any idea?

thanks~
Last edited on
closed account (STR9GNh0)
no assistance?
closed account (S6k9GNh0)
Did you know, that in memory, your variable is held as raw data which is represented as binary? So, in all actuality, your variable is already in binary.

Also, did you know that there isn't a direct way to print this out? Well, you can but its not very fast or as fast as it should be. Our bitset container actually helps us with this but I'm not sure of the common methods it uses:

http://codepad.org/JiOvAY8C

EDIT: Here's an example with templates: http://codepad.org/K685Y208
Careful though, it errors if you give it a type that can't handle the integer you gave which is actually a good thing.

EDIT2: I misread the question...
Last edited on
closed account (STR9GNh0)
i know how to print in both reverse and correct way.

my concern now is - how to change the output binary answer to complement 1 or 2 and print it out~
Last edited on
Complement to one: just invert the bits (xor 111111....).
Complement to two: invert the bits and add one. If you want to do it on the fly, pass the carry as an extra parameter and do primary arithmetic
Last edited on
closed account (STR9GNh0)
thats the problem, when i try to invert i got myself weird symbols or only 1 '0' is being print out
Post your code. ~n will invert all the bits of n, but I guess that you are operating one bit at the time so try n xor 1.
closed account (STR9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
char mapping (int digit)
{
	char letter = HEXADECIMAL [digit];
	
	return letter;
}

int conversion(char a [], int intGiven ,int base)
{
	int counter = 0;
	int trial = intGiven;
	do
	{
		trial = trial / base;
		counter++;
	}while( trial != 0);
	
	for(int i = 0; i < counter; i++)
	{
		int remain = intGiven % base;
		
		a [i] = mapping (remain);	  
		
		intGiven = intGiven / base; 
	}
	
	return counter;
}

void printNumber(const char a [], int counter)
{	   	   
	for (int i = counter-1; i >= 0; i--)
		cout << a [i] << "";
	cout << endl;
}
Traverse your array.
'invert' every cell. How much do I need to be the greatest digit of my base?
Now you have complement to 1(in base 2) or complement to 9 (in base 10)
if you add 1 (in that base), you will have the complement to base.
closed account (STR9GNh0)
sorry but i don't quite get you.

the issue is my above i am able to convert to any base... so if i were to traverse it will it have any effect to my actual results and the result after complement?

i need to have

1. actual result (be it base 2/8/10/16)
2. after complement 1 (in binary)
3. after complement 2 (in binary)
Last edited on
Is this what you want? (using 1 byte)
			10 		2	 	8 		16
number forty two	42		00101010	052		2A
complement 1		213		11010101	325		D5
complement 2		214		11010110	326		D6
Last edited on
closed account (STR9GNh0)
yeap. something like this~
For the binary representation of complement 1 you just need to invert (0->1 and 1->0).
For complement 2 you add one to the complement 1(using a carry).

For convert to the other bases, you could pass trough base 10, and with that number to 8 and 16
closed account (STR9GNh0)
sorry but could you like point to me which part of my code i need to implement this?

i know how it should be done via layman's term but i can't seem to implement it correctly. sorry and thanks~

the only way so far i've done to get the negation result is the change my array hexadecimal to 10 instead of 01.

but i think there is a better way to implement it yahs?
Last edited on
Do the mapping when you need to print.
1
2
//a [i] = mapping (remain);
a[i] = remain;

Now the inversion is simply a[i] = base - (a[i]+1); (another function)
closed account (STR9GNh0)
i tried but failed, any idea why is that so ?
closed account (STR9GNh0)
nvm i found a different method already from what you've mentioned. after 6hours. thanks anyway.
Last edited on
Topic archived. No new replies allowed.