Skip to main content
GameDev.net gamedev.net
🔒 Locked

exclusive or

Started by ECKILLER Sep 5, 2000 at 12:59 PM 1 replies 850+ views
Original Post
ECKILLER
ECKILLER
Hi, Ok in my project for school the book says i need to add 2 32 bit integers using bit by bit addition modulo 2(that is, they are combined using the exclusive or operation). Is this as easy as: int nSumOfBits = intOne ^ intTwo; ????????? If not can someone tell me how to do what my book says i need to do. Please explain and dont just post code as, bit operations aren''t my favorite thing in the world. Thanks ECKILLER
ECKILLER
Arjan
Arjan
No, it isn''t as easy as that. You can''t do it with just exclusive or, you also need and. Let''s say you''ve got two numbers you want to add this way, n1 and n2. You need to calculate the result bit by bit by performing an exclusive or (xor) from the right to the left. You just xor two bits and keep the result. If both bits were 1, then you need to remember 1. If not, just remember a zero.

Now go one bit to the left. Xor the bit of n1 with the bit you just remembered and keep the result (temporary). If both bits were 1, you need to remember one again, if not, remember zero. Now xor the result with the bit of n2 and now you can keep the result. Remember the number you need to and repeat this last paragraph until you''re done.

An example: add 1001 to 0011 (=1100 =12)
from right to left:
bit 0: 1^1=0 so resulting bit is 0. 1 & 1 = 1 so remember 1

bit 1: 0^1=1 so resulting temp bit is 1. 0 & 1 = 0 so temporarily remember 0. 1 (temp bit) ^ 1 (remembered bit) = 0 so resulting bit is zero. New remembered bit is 1 because both bits were 1.

bit 2: 0^0=0 so resulting temp bit is 0. 0 & 0 = 0 so temp remember 0. 0 (temp bit) ^ 1 (remembered bit) = 1 so resulting bit is 1. New remembered bit is 0, because only one bit was set.

bit 3: 1^0=1 so resulting temp bit is 1. 1 & 0 = 0 so temp remember 0. 1 (temp bit) ^ 0 (remembered bit) = 1 so resulting bit is 1. New remembered bit is 0, because only one bit was set.

So the result is (again): 1100
You don''t really need to remember bits when calculating the last bit, only if you need to check for overflows.

I hope this all is correct and I hope you understand it...
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
Stoffel
Stoffel
Sounds like it wants you to write a full-adder chain. In digital logic, a half-adder is:
inputs: carry_in, data_in
outputs: data_out, carry_out
data_out = not (carry_in xor data_in)
carry_out = data_in and carry_in

A full-adder is a chain of two of these modules connecting the first half's carry_out to the second half's carry_in, and the data of the second number to add to the data_in.

A full-adder chain is a chain of full-adders, with each carry_out tied to each carry_in.

Yes, more than you wanted to know. But that's why I'm here.


Edited by - Stoffel on September 5, 2000 5:23:04 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.