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

猜您喜欢...

  1. Daily Interview Problem: Word Ordering in a Different Alphabetical Order
  2. Daily Interview Problem: Contiguous Subarray with Maximum Sum
  3. Daily Interview Problem: Longest Substring With K Distinct Characters
  4. A trick for getting good at coding interviews FASTER
  5. Kaprekar
  6. Daily Interview Problem: Decode String
  7. Algorithm Interview: Maximum Path Sum in Binary Tree
  8. Compare Version Numbers
  9. Daily Interview Problem: Maximum Profit From Stocks
  10. Fibonacci coding

评论 (0)

您的邮件 (不公开域名部分):

您的评论:

悄悄话(发邮件) 颜色 更多笑脸 S x y @

验证码 (点击图片更新):

    当前页暂时没有评论。


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