To the Top
File:  root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 716 Views, 23454 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 | 716 Views, 23454 Search Bots | 142 Words Subscribe to Feed Burner

Related Articles

  1. Algorithm Interview: No Adjacent Repeating Characters
  2. Daily Interview Problem: Merge K Sorted Linked Lists
  3. Daily Interview Problem: Find the Number of Islands
  4. Two-Sum
  5. Patterns for breaking down questions you haven
  6. Find Missing Numbers in an Array
  7. [Daily Problem] Remove Consecutive Nodes that Sum to 0
  8. Daily Interview Question: Longest Sequence with Two Unique Numbers
  9. Sort a Partially Sorted List
  10. Daily Interview Problem: Deepest Node in a Binary Tree

Comments (0)

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">