页面顶部 Top
文件:  root - text - computing - delphi - 2011 - 06 - tricks.txt
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 720 次阅读, 23647 次搜索 | 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, | 英文 | 主页 | 类别: 计算机科学 | 720 次阅读, 23647 次搜索 | 142 个单词 定阅此目录的博客

猜您喜欢...

  1. Autorun.inf Virus Protection
  2. Two Tricks of Delphi
  3. Batch Programming in XP
  4. 56 Bytes
  5. Daily Interview Question: Edit Distance
  6. Making a Height Balanced Binary Search Tree
  7. Algorithm Interview: Permutations of numbers
  8. Binary Tree Level with Minimum Sum
  9. Daily Interview Problem: Find the k-th Largest Element in a List
  10. Daily Interview Puzzle: Ways to Traverse a Grid

评论 (0)

    当前页暂时没有评论。


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