From 5fdaf349d6058f826b4f578d61b8962c23156df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BC=AF=E5=A8=81=20Po-Wei=20Chou?= Date: Thu, 5 May 2016 17:17:22 -0400 Subject: [PATCH] Update README.md --- README.md | 87 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 80080d8..785cf12 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,7 @@ sign = -(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1)); sign = v >> (sizeof(int) * CHAR_BIT - 1); ``` -The last expression above evaluates to sign = v >> 31 -for 32-bit integers. -This is one operation faster than the obvious way, sign = -(v < 0). -This trick works because when signed integers are shifted right, the value -of the far left bit is copied to the other bits. The far left bit is 1 -when the value is negative and 0 otherwise; all 1 bits gives -1. -Unfortunately, this behavior is architecture-specific. +The last expression above evaluates to `sign = v >> 31` for 32-bit integers. This is one operation faster than the obvious way, `sign = -(v < 0)`. This trick works because when signed integers are shifted right, the value of the far left bit is copied to the other bits. The far left bit is 1 when the value is negative and 0 otherwise; all 1 bits gives -1. Unfortunately, this behavior is architecture-specific. Alternatively, if you prefer the result be either -1 or +1, then use: @@ -127,27 +121,20 @@ sign = (v != 0) | (v >> (sizeof(int) * CHAR_BIT - 1)); // -1, 0, or +1 // Or, for portability, brevity, and (perhaps) speed: sign = (v > 0) - (v < 0); // -1, 0, or +1 ``` -If instead you want to know if something is non-negative, resulting in +1 or -else 0, then use: +If instead you want to know if something is non-negative, resulting in +1 or else 0, then use: ```c sign = 1 ^ ((unsigned int)v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then 0, else 1 ``` -Caveat: On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C -specification leaves the result of signed right-shift implementation-defined, -so on some systems this hack might not work. For greater portability, -Toby Speight suggested on September 28, 2005 that CHAR_BIT be used here -and throughout rather than assuming bytes were 8 bits long. Angus recommended -the more portable versions above, involving casting on March 4, 2006. -Rohit Garg suggested the version -for non-negative integers on September 12, 2009. +Caveat: On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. For greater portability, Toby Speight suggested on September 28, 2005 that CHAR_BIT be used here and throughout rather than assuming bytes were 8 bits long. Angus recommended the more portable versions above, involving casting on March 4, 2006. [Rohit Garg](http://rpg-314.blogspot.com/) suggested the version for non-negative integers on September 12, 2009.
-### Detect if two integers have opposite signs + +### Detect if two integers have opposite signs @@ -506,7 +493,9 @@ April 3, 2007 and alerted me to a typo 2 days later.
-### Conditionally negate a value without branching + +### Conditionally negate a value without branching + If you need to negate only when a flag is false, then use the following @@ -540,7 +529,8 @@ missing on November 26, 2009, and received a bug bounty. -### Merge bits from two values according to a mask + +### Merge bits from two values according to a mask @@ -1748,7 +1738,8 @@ brought this oversight to my attention on December 12, 2003.
-### Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup + +### Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup @@ -1954,7 +1945,8 @@ and he suggested using memcpy.
-### Count the consecutive zero bits (trailing) on the right linearly + +### Count the consecutive zero bits (trailing) on the right linearly @@ -1986,7 +1978,8 @@ I had neglected to paste the unsigned modifier for v.
-### Count the consecutive zero bits (trailing) on the right in parallel + +### Count the consecutive zero bits (trailing) on the right in parallel @@ -2014,7 +2007,8 @@ February 4, 2011.
-### Count the consecutive zero bits (trailing) on the right by binary search + +### Count the consecutive zero bits (trailing) on the right by binary search @@ -2072,7 +2066,8 @@ subtracting at the end).
-### Count the consecutive zero bits (trailing) + +### Count the consecutive zero bits (trailing) on the right by casting to a float @@ -2095,7 +2090,8 @@ If v is zero, then the result is -127.
-### Count the consecutive zero bits (trailing) + +### Count the consecutive zero bits (trailing) on the right with modulus division and lookup @@ -2127,7 +2123,8 @@ the table values, and found it was invented earlier by Reiser, according to
-### Count the consecutive zero bits (trailing) + +### Count the consecutive zero bits (trailing) on the right with multiply and lookup @@ -2164,7 +2161,8 @@ compiled with 64-bit ints.
-### Round up to the next highest power of 2 by float casting + +### Round up to the next highest power of 2 by float casting @@ -2208,7 +2206,8 @@ rounding, but it used one more operation.
-### Round up to the next highest power of 2 + +### Round up to the next highest power of 2 @@ -2258,7 +2257,8 @@ where they arrive at the same algorithm.
-### Interleave bits the obvious way + +### Interleave bits the obvious way @@ -2282,7 +2282,8 @@ another if their x and y values are close.
-### Interleave bits by table lookup + +### Interleave bits by table lookup @@ -2343,7 +2344,8 @@ only need 11 operations total.
-### Interleave bits with 64-bit multiply + +### Interleave bits with 64-bit multiply In 11 operations, this version interleaves bits of two bytes @@ -2370,7 +2372,8 @@ October 10, 2004 after reading the multiply-based bit reversals here.
-### Interleave bits by Binary Magic Numbers + +### Interleave bits by Binary Magic Numbers @@ -2400,7 +2403,8 @@ z = x | (y << 1);
-### Determine if a word has a zero byte + +### Determine if a word has a zero byte @@ -2478,7 +2482,8 @@ on April 27, 1987 by Alan Mycroft.
-### Determine if a word has a byte equal to n + +### Determine if a word has a byte equal to n @@ -2500,7 +2505,8 @@ for `haszero`.
-### Determine if a word has a byte less than n + +### Determine if a word has a byte less than n @@ -2531,7 +2537,8 @@ April 10, 2005, inspired by Juha's `countmore`, below.
-### Determine if a word has a byte greater than n + +### Determine if a word has a byte greater than n @@ -2556,7 +2563,8 @@ April 6, 2005, and he added `countmore` on April 8, 2005.
-### Determine if a word has a byte between m and n + +### Determine if a word has a byte between m and n @@ -2599,7 +2607,8 @@ Sean Anderson created `hasbetween` and
-### Compute the lexicographically next bit permutation + +### Compute the lexicographically next bit permutation