wu :: forums
« wu :: forums - CS: Increment without + or - »

Welcome, Guest. Please Login or Register.
May 18th, 2024, 3:19am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: towr, william wu, SMQ, ThudnBlunder, Icarus, Eigenray, Grimbal)
   CS: Increment without + or -
« Previous topic | Next topic »
Pages: 1 2  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: CS: Increment without + or -  (Read 4976 times)
greek_genius
Newbie
*





   


Posts: 1
Solution  
« Reply #25 on: Oct 27th, 2004, 10:08am »
Quote Quote Modify Modify

The solution is simple:
 
for 32 bit integers:  a+1 = ~a * 0xFFFFFFFF
 
general solution:
 
 a+1 = ~a  * ~0;
 
 
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Solution  
« Reply #26 on: Oct 28th, 2004, 2:16am »
Quote Quote Modify Modify

on Oct 27th, 2004, 10:08am, greek_genius wrote:

 a+1 = ~a  * ~0;

Cool!
 
From now on, I'll use it in all my programs  Grin
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: CS: Increment without + or -  
« Reply #27 on: Oct 28th, 2004, 5:41am »
Quote Quote Modify Modify

I wouldn't mind seeing some proof, as it's not that obvious to me..
« Last Edit: Oct 28th, 2004, 5:47am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
John_Gaughan
Uberpuzzler
*****



Behold, the power of cheese!

5187759 5187759   john23874   SnowmanJTG
WWW Email

Gender: male
Posts: 767
Re: CS: Increment without + or -  
« Reply #28 on: Oct 28th, 2004, 8:37am »
Quote Quote Modify Modify

on Oct 28th, 2004, 5:41am, towr wrote:
I wouldn't mind seeing some proof, as it's not that obvious to me..

This solution looks like multiplying two one's complement numbers, and it is, but try looking at it differently. Think of it as repeated addition of a one's complement number, truncating the overflowed MSBs.
 
Given a number 1001, adding one gives 1010.
~1001 = 0110
Repeated addition gives
1001 + 1 = 0110 + 1100 + 1000 + 0000 = 1 1010
 
if x is the number:
~x * ~0 = [smiley=sum.gif] n=0 -> bitlength(x) (~x << n)
 
The first digit is simply the one's complement and is not added to anything else, so it is simply the inverse of the original. The next digit takes that digit's complement, adds one, and carries from the first digit... kinda like a full adder, huh?
 
Do this enough times and you turn a typical serial multiplier circuit and turn it into a multi-bit adder because of that one's complement.
« Last Edit: Oct 28th, 2004, 11:19am by John_Gaughan » IP Logged

x = (0x2B | ~0x2B)
x == the_question
Barukh
Uberpuzzler
*****






   


Gender: male
Posts: 2276
Re: CS: Increment without + or -  
« Reply #29 on: Oct 28th, 2004, 10:10am »
Quote Quote Modify Modify

on Oct 28th, 2004, 5:41am, towr wrote:
I wouldn't mind seeing some proof, as it's not that obvious to me..

Let me also try...
 
I looked at this as ordinary multiplication. The only thing to observe is that on the n-bit machine, arithmetic operations are performed modulo 2n. Therefore:
 
a + ~a = 2n - 1 [equiv] -1,
~0 = 2n - 1 [equiv] -1,
 
and  
 
~a * ~0 [equiv] (-a-1) * (-1) = a+1.
 
This is a real little gem!
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: CS: Increment without + or -  
« Reply #30 on: Oct 28th, 2004, 10:21am »
Quote Quote Modify Modify

on Oct 28th, 2004, 10:10am, Barukh wrote:
~a * ~0 [equiv] (-a-1) * (-1) = a+1.
Ah yes, of course.. I should have thought of that myself..
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Pages: 1 2  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board