[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

702.0. "f(A) -> C, f(B) -> D" by CLT::GILBERT (eager like a child) Fri May 08 1987 13:15

I have two integers A and B, and another two C and D.  I'd like to find
a very simple function f such that f(A) = C and f(B) = D.  This simple
function will be implemented with arithmetic addition and subtraction,
bit-wise logical operations, logical shifts and any necessary constants
(which would be pre-computed from A, B, C, and D).

The fewer operations, the better.  How can it be done?
T.RTitleUserPersonal
Name
DateLines
702.1BEING::POSTPISCHILAlways mount a scratch monkey.Fri May 08 1987 14:265
    I can guarantee five or fewer operations, given a two's complement
    machine.
    
    
    				-- edp 
702.2not so simpleVINO::JMUNZERFri May 08 1987 14:2828
Pick some bit 2^k that's different in A and B.  Let Rk (x) be a k-bit right
shift of x (zero-filled).

f(x) = AND (0 - Rk ( AND ( XOR (x, B), 2^k) ), C) +
       AND (0 - Rk ( AND ( XOR (x, A), 2^k) ), D)

Then:
    
f(A) = AND (0 - Rk ( AND ( XOR (A, B), 2^k) ), C) +
       AND (0 - Rk ( AND ( XOR (A, A), 2^k) ), D)

     = AND (0 - Rk ( AND ( 2^k + other bits, 2^k) ), C) +
       AND (0 - Rk ( AND (0, 2^k) ), D)

     = AND (0 - Rk (2^k), C) +
       AND (0 - Rk (0), D)

     = AND (0 - 1, C) +
       AND (0 - 0, D)

     = AND (-1, C) +
       AND (0, D)

     = C
    
    [assuming twos complement arithmetic]
    
    John
702.3SSDEVO::LARYSun May 10 1987 04:5811
Even fewer operations would be: (using the notation of .-1):

f(x) = AND (0 - AND ( Rk(x), 1), DIFF) + BASE

Where BASE and DIFF are pre-computed constants with the values:

	BASE = C and DIFF = D-C if bit 2^k is set in B but not in A

	BASE = D and DIFF = C-D if bit 2^k is set in A but not in B