Блог пользователя Frez

Автор Frez, история, 11 лет назад, По-английски

Hi.

I'm very interested in binary representation of numbers.I see some attractive formula and methods such as :

  • (x)&(-x) return smallest significant of x that is 1 !

  • use binary for create all subsets of a small set :)

  • ...

I want to learn more, so create this blog for sharing interest knowledges and making it worth post for competitive programmers. also I want to know is there a relation about remainder of numbers or divisibility of them in binary ? ( for example in decimal we know a number is divisible by 5 when it's last digit is 0 or 5 )

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится
int bitCount(int i)
{
	i = i - ((i >> 1) & 0x55555555);
	i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
	return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

Count the number of ones in the binary representation of i.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Frez (previous revision, new revision, compare).

»
11 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится +2 Проголосовать: не нравится

for set/turn on the j-th item of the set, use to bitwise OR operation S |= (1<<j) S = 5 (base 10) = 101 (base 2) j = 3 (base 10) = 011 (base 2) ------------- OR S = 7 (base 10) = 111 (base 2) --------------------------- To check if the j'th item of the set is on,use the bitwise AND operation P = S & (1<<j) S = 5 (base 10) = 101 (base 2) j = 2 , 1<<j = 100 (base 2) ------------- AND p = 8 (base 10) = 100 no Ziro ,the 3rd item is 1 --------------------------- multiply & divide by 2 s*2 --> s<<1 s/2 --> s>>1 s/pow(2,n) --> s>>n s*pow(2,n) --> s<<n ---------------------------
»
11 лет назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

You can find many tricks in Hacker's Delight book by Henry S. Warren.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится
»
11 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

A good article-sized read is the relevant TopCoder tutorial by bmerry.

For a more thorough study, the Hacker's Delight book mentioned before is the way to go.