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

Related Articles

  1. Daily Interview Question: Create a Simple Calculator
  2. Batch Programming in XP
  3. [Daily Problem] Remove Consecutive Nodes that Sum to 0
  4. [Daily Problem] Course Prerequisites
  5. Daily Interview Puzzle: Intersection of Linked Lists
  6. Algorithm Interview: Longest Consecutive Sequence
  7. Algorithm Interview Question: Nth Fibonacci Number
  8. Daily Interview Question: Edit Distance
  9. Daily Interview Problem: Decode String
  10. CVE-2015-8874 - cPanel EasyApache Vulnerabilities

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