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

猜您喜欢...

  1. Daily Interview Problem: Min Range Needed to Sort
  2. Daily Interview Question: Create a Simple Calculator
  3. Daily Interview Problem: Distribute Bonuses
  4. Non-decreasing Array with Single Modification
  5. Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
  6. 56 Bytes
  7. Algorithm Interview: Longest Consecutive Sequence
  8. Daily Interview Puzzle: Falling Dominoes
  9. Algorithm Interview: Maximum Path Sum in Binary Tree
  10. Daily Interview Problem: Merge Overlapping Intervals

评论 (0)

    当前页暂时没有评论。


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