To the Top
File:  root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 720 Views, 23701 Search Bots | 142 Words

Subscribe to Feed Burner | Browse | Archive
Two tricks of making integer computation faster in Delphi.

1. Two integers a and b, to compute a mod b, where b is power of two (two, four, eight ... etc)...

in delphi, you write
result := a mod b;
in this case, to make it faster,
you can write
result := a and (b - 1); // b >= 2, and power of 2.

2. Two integers a (a >= 0) and b, to compute a div b, where b is power of two (two, four, eight ... etc)...

in delphi, you write
result := a div b;
in this case, to make it faster,
you can write
c := fun(b); // fun returns the integer(log2(b))
result := a shr c; // where b >= 2, and power of 2

// returns the left-most significant digit.
function fun(b: integer): integer; assembler; register;
asm
bsr eax, eax
end;

if (a < 0), it can be reworked as result = -((-a) shr c)
Tags: 计算机, Computing Tips, | English | Home Page | Cateogry: Computing | 720 Views, 23701 Search Bots | 142 Words Subscribe to Feed Burner

Related Articles

  1. Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
  2. Algorithm Interview: Make the Largest Number
  3. Algorithm Interview Question: H-Index
  4. Spreadsheet Column Title
  5. Skip the readings, focus on problems. And use all the hints!
  6. Algorithm Interview: Subarray With Target Sum
  7. Staying on a Chess Board
  8. Two Tricks of Delphi
  9. New Vulnerabilities (CVE-2016-4581) have been detected in CentOS/RHEL/CloudLinux 7
  10. Sorting a list with 3 unique numbers

Comments (0)

    Be the first one to comment this page !


Page Edited: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">