页面顶部 Top
文件:  root - text - computing - delphi - 2011 - 06 - tricks.txt
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 716 次阅读, 23307 次搜索 | 142 个单词

定阅此目录的博客 | 浏览 | 博客存档
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)
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 716 次阅读, 23307 次搜索 | 142 个单词 定阅此目录的博客

猜您喜欢...

  1. Algorithm Interview: Convert Roman Numerals to Decimal
  2. Daily Interview Problem: Jump to the End
  3. Compare Version Numbers
  4. [Daily Problem] Course Prerequisites
  5. Daily Interview Problem: Group Words that are Anagrams
  6. Algorithm Interview Question: H-Index
  7. Longest Substring Without Repeating Characters
  8. [Daily Problem] Angles of a Clock
  9. Daily Interview Question: Longest Sequence with Two Unique Numbers
  10. Algorithm Interview Question: Symmetric k-ary Tree

评论 (0)

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="索引, 跟踪">