modusTollens

Challenge: LOGIC001

2 posts in this topic

It seems to me that at least a few of you would have the background to complete this task:

Using only bitwise and logical operators, construct a full-adder that will compute the sum of two integers. For simplicity's sake, we can limit the size of the integers to 8 bits, and not worry about sign, or overflow. The result can just be truncated.

Use any language you like, if you choose a non-assembly language, you may use comparators to help with a loop structure.

eg:

10010010

+10101011

00111101

0

Share this post


Link to post
Share on other sites

8 bit adder -- In Perl...


#!/usr/bin/perl
my ($value1,$value2) = @ARGV;
$carry=$sum=0;
$numOfBits = 8;

for ($bit = 0; $bit < $numOfBits; ++$bit) {
$bitValue = 1 << $bit;
$bitA = $value1 & $bitValue;
$bitB = $value2 & $bitValue;
$sum = $sum | ( ( $bitA ^ $bitB ) ^ $carry );
$carry = ( ( $bitA & $bitB) | ( $carry & ( $bitA ^ $bitB ) ) ) << 1;
}
print $sum;

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now