Results 1 to 10 of 10

Thread: functions w/ bit manipulation

  1. #1
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default functions w/ bit manipulation

    your basic add,subtract,multiply,divide operations using only shifts,bitwise operations. not meant for use, just for fun. note that my division wasn't particularly elegant and you won't get corresponding values for divide(x,y) to x/y in rare cases like $8000000/y (actually mine is more accurate). other than that everything else seems to check out. enjoy.

    SCAR Code:
    function Add(A, B: Integer): Integer;
    begin
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := Result and A shl 1;
      A := B xor Result;
      B := B and Result shl 1;
      Result := A xor B;
      A := A and B shl 1;
      B := Result xor A;
      Result := B;
    end;

    function Subtract(A, B: Integer): Integer;
    begin
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      B := B and A shl 1;
      A := A xor B;
      Result := A;
    end;

    function Multiply(A, B: Integer): Integer;
    var
      C: Integer;
    begin
      C := 1431655765 xor 1431655765 shl (A and 1);
      Result := Add(Result, B and C);
      C := 1431655765 xor 1431655765 shl (A shr 1 and 1);
      Result := Add(Result, B and C shl 1);
      C := 1431655765 xor 1431655765 shl (A shr 2 and 1);
      Result := Add(Result, B and C shl 2);
      C := 1431655765 xor 1431655765 shl (A shr 3 and 1);
      Result := Add(Result, B and C shl 3);
      C := 1431655765 xor 1431655765 shl (A shr 4 and 1);
      Result := Add(Result, B and C shl 4);
      C := 1431655765 xor 1431655765 shl (A shr 5 and 1);
      Result := Add(Result, B and C shl 5);
      C := 1431655765 xor 1431655765 shl (A shr 6 and 1);
      Result := Add(Result, B and C shl 6);
      C := 1431655765 xor 1431655765 shl (A shr 7 and 1);
      Result := Add(Result, B and C shl 7);
      C := 1431655765 xor 1431655765 shl (A shr 8 and 1);
      Result := Add(Result, B and C shl 8);
      C := 1431655765 xor 1431655765 shl (A shr 9 and 1);
      Result := Add(Result, B and C shl 9);
      C := 1431655765 xor 1431655765 shl (A shr 10 and 1);
      Result := Add(Result, B and C shl 10);
      C := 1431655765 xor 1431655765 shl (A shr 11 and 1);
      Result := Add(Result, B and C shl 11);
      C := 1431655765 xor 1431655765 shl (A shr 12 and 1);
      Result := Add(Result, B and C shl 12);
      C := 1431655765 xor 1431655765 shl (A shr 13 and 1);
      Result := Add(Result, B and C shl 13);
      C := 1431655765 xor 1431655765 shl (A shr 14 and 1);
      Result := Add(Result, B and C shl 14);
      C := 1431655765 xor 1431655765 shl (A shr 15 and 1);
      Result := Add(Result, B and C shl 15);
      C := 1431655765 xor 1431655765 shl (A shr 16 and 1);
      Result := Add(Result, B and C shl 16);
      C := 1431655765 xor 1431655765 shl (A shr 17 and 1);
      Result := Add(Result, B and C shl 17);
      C := 1431655765 xor 1431655765 shl (A shr 18 and 1);
      Result := Add(Result, B and C shl 18);
      C := 1431655765 xor 1431655765 shl (A shr 19 and 1);
      Result := Add(Result, B and C shl 19);
      C := 1431655765 xor 1431655765 shl (A shr 20 and 1);
      Result := Add(Result, B and C shl 20);
      C := 1431655765 xor 1431655765 shl (A shr 21 and 1);
      Result := Add(Result, B and C shl 21);
      C := 1431655765 xor 1431655765 shl (A shr 22 and 1);
      Result := Add(Result, B and C shl 22);
      C := 1431655765 xor 1431655765 shl (A shr 23 and 1);
      Result := Add(Result, B and C shl 23);
      C := 1431655765 xor 1431655765 shl (A shr 24 and 1);
      Result := Add(Result, B and C shl 24);
      C := 1431655765 xor 1431655765 shl (A shr 25 and 1);
      Result := Add(Result, B and C shl 25);
      C := 1431655765 xor 1431655765 shl (A shr 26 and 1);
      Result := Add(Result, B and C shl 26);
      C := 1431655765 xor 1431655765 shl (A shr 27 and 1);
      Result := Add(Result, B and C shl 27);
      C := 1431655765 xor 1431655765 shl (A shr 28 and 1);
      Result := Add(Result, B and C shl 28);
      C := 1431655765 xor 1431655765 shl (A shr 29 and 1);
      Result := Add(Result, B and C shl 29);
      C := 1431655765 xor 1431655765 shl (A shr 30 and 1);
      Result := Add(Result, B and C shl 30);
      C := 1431655765 xor 1431655765 shl (A shr 31 and 1);
      Result := Add(Result, B and C shl 31);
    end;

    function Compare(A, B: Integer): Integer;
    var
      C, D: Integer;
    begin
      C := A xor B;
      Result := 1;
      D := 1431655765 xor 1431655765 shl (C shr 1 and 1);
      Result := 1 and D;
      D := 1431655765 xor 1431655765 shl (C shr 2 and 1);
      Result := (2 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 3 and 1);
      Result := (3 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 4 and 1);
      Result := (4 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 5 and 1);
      Result := (5 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 6 and 1);
      Result := (6 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 7 and 1);
      Result := (7 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 8 and 1);
      Result := (8 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 9 and 1);
      Result := (9 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 10 and 1);
      Result := (10 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 11 and 1);
      Result := (11 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 12 and 1);
      Result := (12 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 13 and 1);
      Result := (13 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 14 and 1);
      Result := (14 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 15 and 1);
      Result := (15 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 16 and 1);
      Result := (16 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 17 and 1);
      Result := (17 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 18 and 1);
      Result := (18 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 19 and 1);
      Result := (19 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 20 and 1);
      Result := (20 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 21 and 1);
      Result := (21 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 22 and 1);
      Result := (22 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 23 and 1);
      Result := (23 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 24 and 1);
      Result := (24 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 25 and 1);
      Result := (25 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 26 and 1);
      Result := (26 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 27 and 1);
      Result := (27 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 28 and 1);
      Result := (28 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 29 and 1);
      Result := (29 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 30 and 1);
      Result := (30 and D) or (Result and not D);
      D := 1431655765 xor 1431655765 shl (C shr 31 and 1);
      Result := (31 and D) or (Result and not D);
      Result := (A shr Result and 1) or (B shr Result and 1 xor 1);
    end;

    function Divide(A, B: Integer): Integer;
    var
      C, D: Integer;
    begin
      C := 1431655765 xor 1431655765 shl ((A xor B) shr 31);
      A := Add(A xor (1431655765 xor 1431655765 shl
           (A shr 31)), A shr 31);
      B := Add(B xor (1431655765 xor 1431655765 shl
           (B shr 31)), B shr 31);
      D := Compare(A, B shl 30) and Compare(B and 3, B);
      Result := Result or D shl 30;
      A := Subtract(A, B shl 30 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 29) and Compare(B and 7, B);
      Result := Result or D shl 29;
      A := Subtract(A, B shl 29 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 28) and Compare(B and 15, B);
      Result := Result or D shl 28;
      A := Subtract(A, B shl 28 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 27) and Compare(B and 31, B);
      Result := Result or D shl 27;
      A := Subtract(A, B shl 27 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 26) and Compare(B and 63, B);
      Result := Result or D shl 26;
      A := Subtract(A, B shl 26 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 25) and Compare(B and 127, B);
      Result := Result or D shl 25;
      A := Subtract(A, B shl 25 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 24) and Compare(B and 255, B);
      Result := Result or D shl 24;
      A := Subtract(A, B shl 24 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 23) and Compare(B and 511, B);
      Result := Result or D shl 23;
      A := Subtract(A, B shl 23 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 22) and Compare(B and 1023, B);
      Result := Result or D shl 22;
      A := Subtract(A, B shl 22 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 21) and Compare(B and 2047, B);
      Result := Result or D shl 21;
      A := Subtract(A, B shl 21 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 20) and Compare(B and 4095, B);
      Result := Result or D shl 20;
      A := Subtract(A, B shl 20 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 19) and Compare(B and 8191, B);
      Result := Result or D shl 19;
      A := Subtract(A, B shl 19 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 18) and Compare(B and 16383, B);
      Result := Result or D shl 18;
      A := Subtract(A, B shl 18 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 17) and Compare(B and 32767, B);
      Result := Result or D shl 17;
      A := Subtract(A, B shl 17 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 16) and Compare(B and 65535, B);
      Result := Result or D shl 16;
      A := Subtract(A, B shl 16 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 15) and Compare(B and 131071, B);
      Result := Result or D shl 15;
      A := Subtract(A, B shl 15 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 14) and Compare(B and 262143, B);
      Result := Result or D shl 14;
      A := Subtract(A, B shl 14 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 13) and Compare(B and 524287, B);
      Result := Result or D shl 13;
      A := Subtract(A, B shl 13 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 12) and Compare(B and 1048575, B);
      Result := Result or D shl 12;
      A := Subtract(A, B shl 12 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 11) and Compare(B and 2097151, B);
      Result := Result or D shl 11;
      A := Subtract(A, B shl 11 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 10) and Compare(B and 4194303, B);
      Result := Result or D shl 10;
      A := Subtract(A, B shl 10 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 9) and Compare(B and 8388607, B);
      Result := Result or D shl 9;
      A := Subtract(A, B shl 9 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 8) and Compare(B and 16777215, B);
      Result := Result or D shl 8;
      A := Subtract(A, B shl 8 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 7) and Compare(B and 33554431, B);
      Result := Result or D shl 7;
      A := Subtract(A, B shl 7 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 6) and Compare(B and 67108863, B);
      Result := Result or D shl 6;
      A := Subtract(A, B shl 6 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 5) and Compare(B and 134217727, B);
      Result := Result or D shl 5;
      A := Subtract(A, B shl 5 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 4) and Compare(B and 268435455, B);
      Result := Result or D shl 4;
      A := Subtract(A, B shl 4 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 3) and Compare(B and 536870911, B);
      Result := Result or D shl 3;
      A := Subtract(A, B shl 3 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 2) and Compare(B and 1073741823, B);
      Result := Result or D shl 2;
      A := Subtract(A, B shl 2 and (1431655765 xor 1431655765 shl D));
      D := Compare(A, B shl 1) and Compare(B and 2147483647, B);
      Result := Result or D shl 1;
      A := Subtract(A, B shl 1 and (1431655765 xor 1431655765 shl D));
      Result := Result or Compare(A, B);
      Result := Add(Result xor C, C and 1);
    end;

  2. #2
    Join Date
    Nov 2006
    Location
    NSW, Australia
    Posts
    3,487
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Holy crap....?! You read all this on Wiki?!
    [CENTER][img]http://signatures.mylivesignature.com/54486/113/4539C8FAAF3EAB109A3CC1811EF0941B.png[/img][/CENTER]
    [CENTER][BANANA]TSN ~ Vacation! ~ says :I Love Santy[/BANANA][/CENTER]

    [CENTER][BANANA]Raymond - Oh rilie? says :Your smart[/BANANA][/CENTER]

  3. #3
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Instant overkill here.

    Edit:
    Someone call Benland!

    Edit:
    Im sure that actual pascal/delphi math source doesn't look like this. Maybe bit shorter
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

  4. #4
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    lol of course its over kill. with if..then, and loops its would be less than 1/10 of the code. and you wont find that sort of code in any source that i know of because add/sub/div/mul are handled by the cpu(alu), not language.

  5. #5
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Fuck, We don't need two ben's... That's twice as much head aches

  6. #6
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Nice work
    Hup Holland Hup!

  7. #7
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Very good, did you read Yakman's Bitshift tutorial?

  8. #8
    Join Date
    Mar 2007
    Posts
    107
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    lol n3ss3s your right on, forgot to credit yakman, thanks yakman!

  9. #9
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    all thoose
    SCAR Code:
    xor
    's and
    SCAR Code:
    shl
    e.t.c
    Makes me feel queasy .

    Good Work

  10. #10
    Join Date
    Feb 2007
    Location
    Het ademt zwaar en moedeloos vannacht.
    Posts
    7,211
    Mentioned
    26 Post(s)
    Quoted
    72 Post(s)

    Default

    Looks good!
    I made a new script, check it out!.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Abstract Photo Manipulation
    By Torrent of Flame in forum Graphics and Multimedia
    Replies: 17
    Last Post: 03-02-2009, 07:13 PM
  2. Quick Tut on Debug Box manipulation
    By n3ss3s in forum OSR Advanced Scripting Tutorials
    Replies: 11
    Last Post: 09-16-2008, 01:31 AM
  3. string variable manipulation?
    By bobert5696 in forum OSR Help
    Replies: 15
    Last Post: 02-24-2007, 06:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •