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

猜您喜欢...

  1. Maximum In A Stack
  2. Daily Interview Problem: Reverse Integer
  3. Batch Programming in XP
  4. Algorithm Interview: Determine If Linked List is Palindrome
  5. Daily Interview Problem: Queue Using Two Stacks
  6. Daily Interview Problem: Find the Number of Islands
  7. Reverse a Linked List
  8. Algorithm Interview Question: Find the Single Element in an Array of Duplicates
  9. Daily Interview Problem: Validate Binary Search Tree
  10. Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree

评论 (0)

    当前页暂时没有评论。


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