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

Related Articles

  1. Reverse a Linked List
  2. Algorithm Interview Question: Find the Single Element in an Array of Duplicates
  3. Daily Interview Problem: Look and Say Sequence
  4. Daily Interview Problem: Reverse Integer
  5. Daily Interview Problem: Longest Substring With K Distinct Characters
  6. Daily Interview Problem: Minimum Removals for Valid Parenthesis
  7. Algorithm Interview: Shifted String
  8. Most Frequent Subtree Sum
  9. [Daily Problem] Longest Palindromic Substring
  10. Kaprekar

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    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">