78
www.gowrikumar.com www.gowrikumar.com Counting the Counting the number of bits set number of bits set CH Gowri Kumar CH Gowri Kumar [email protected] [email protected]

Counting Bits

Embed Size (px)

DESCRIPTION

This ppt describes one of the interesting algorithms to count the number of bits set in an unsigned integer. x = (x & 0x55555555) + ((x>>1)&0x55555555)); x = (x&0x33333333) + ((x>>2)&0x33333333); ... ....

Citation preview

Page 1: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

Counting the number of Counting the number of bits setbits set

CH Gowri KumarCH Gowri Kumar

[email protected]@gmail.com

Page 2: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

Copyright NoticeCopyright Notice

© 2008, © 2008, www.gowrikumar.comwww.gowrikumar.com

You are given unrestricted freedom to You are given unrestricted freedom to

use, modify, copy and redistribute in use, modify, copy and redistribute in

any format/mediaany format/media, as long as you retain , as long as you retain

this copyright statement.this copyright statement.

Page 3: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

Problem StatementProblem Statement

Given an unsigned integer n, count the Given an unsigned integer n, count the number of bits set in it.number of bits set in it.

Ex :Ex : 3 3 11 11 2 2 8 8 1000 1000 1 1 2323101111011144

Page 4: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 5: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 6: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 7: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 8: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 9: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

Page 10: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0

Page 11: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0

Page 12: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

Page 13: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

Page 14: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0

Page 15: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0

Page 16: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0

Page 17: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

Page 18: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1

Page 19: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1

Page 20: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

Page 21: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

+

Page 22: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

+

Page 23: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

1 0 0 1 0 0 0 0 1 1 0 0 0 1

Page 24: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

1 0 0 1 0 0 0 0 1 1 0 0 0 1

+

Page 25: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

1 0 0 1 0 0 0 0 1 1 0 0 0 1

+

Page 26: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 1 0 0 0 0 1 1 0 0 0 1

+

Page 27: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 1 0 0 0 0 1 1 0 0 0 1

+

Page 28: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 0 0 1 1 0 0 0 1

Page 29: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 0 0 1 1 0 0 0 1

+

Page 30: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 0 0 1 1 0 0 0 1

+

Page 31: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 1 1 0 0 0 1

Page 32: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 1 1 0 0 0 1

+

Page 33: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

0 0 1 1 0 0 0 1

+

Page 34: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

1 1 0 0 0 1

Page 35: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1

1 1 0 0 0 1

+

Page 36: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

1 1 0 0 0 1

+

Page 37: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 0 0 1

Page 38: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 0 0 1

+

Page 39: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 0 0 1

+

Page 40: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 1

Page 41: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 1

+

Page 42: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

0 1

+

Page 43: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

Page 44: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 1

+ + + + + + + +

Page 45: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

Page 46: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

C codeC code

x = (x & 0x5555) + ((x>>1) & 0x5555)x = (x & 0x5555) + ((x>>1) & 0x5555)

Page 47: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1

Page 48: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 1 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1+

Page 49: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 0 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1+

Page 50: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 0 1 0

0 1 0 0 0 0 1 0 0 0 0 1

Page 51: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 0 1 0

0 1 0 0 0 0 1 0 0 0 0 1+

Page 52: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 0 0 10 0 1 0

0 1 0 0 0 0 1 0 0 0 0 1+

Page 53: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 0 0 10 0 1 0

0 0 1 0 0 0 0 1

Page 54: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 0 0 10 0 1 0

0 0 1 0 0 0 0 1+

Page 55: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

0 0 1 0 0 0 0 1+

Page 56: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

0 0 0 1

Page 57: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

0 0 0 1+

Page 58: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

0 0 0 1+

Page 59: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

Page 60: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 10 1 0 00 0 1 0

0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1+ + + +

Page 61: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0

Page 62: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

C codeC code

x = (x & 0x5555) + ((x>>1) & 0x5555)x = (x & 0x5555) + ((x>>1) & 0x5555)

x = (x & 0x3333) + ((x>>2) & 0x3333)x = (x & 0x3333) + ((x>>2) & 0x3333)

Page 63: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0 +

Page 64: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0 +

Page 65: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 0 10 0 1 1

0 0 0 10 0 1 0

Page 66: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 0 10 0 1 1

0 0 0 10 0 1 0 +

Page 67: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 1 10 0 0 0

0 0 0 10 0 1 0 +

Page 68: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 1 10 0 0 0

Page 69: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 0 10 0 1 1

0 0 0 10 0 1 00 0 0 10 0 1 0 + +

Page 70: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 1 10 0 0 0

Page 71: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

C codeC code

x = (x & 0x5555) + ((x>>1) & 0x5555)x = (x & 0x5555) + ((x>>1) & 0x5555)

x = (x & 0x3333) + ((x>>2) & 0x3333)x = (x & 0x3333) + ((x>>2) & 0x3333)

x = (x & 0x0F0F) + ((x>>4) & 0x0F0F)x = (x & 0x0F0F) + ((x>>4) & 0x0F0F)

Page 72: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 1 0 00 1 1 0 0 0 0 10 0 1 1

0 0 1 10 0 0 0 0 0 1 10 0 0 0+

Page 73: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 0 0 00 0 0 0 0 1 1 00 0 0 0

0 0 1 10 0 0 0 0 0 1 10 0 0 0+

Page 74: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

0 0 0 00 0 0 0 0 1 1 00 0 0 0

Page 75: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

C codeC code

x = (x & 0x5555) + ((x>>1) & 0x5555)x = (x & 0x5555) + ((x>>1) & 0x5555)

x = (x & 0x3333) + ((x>>2) & 0x3333)x = (x & 0x3333) + ((x>>2) & 0x3333)

x = (x & 0x0F0F) + ((x>>4) & 0x0F0F)x = (x & 0x0F0F) + ((x>>4) & 0x0F0F)

x = (x & 0x00FF) + (x>>16) & 0x00FF)x = (x & 0x00FF) + (x>>16) & 0x00FF)

Page 76: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

Code for 32-bit integersCode for 32-bit integers

x = (x&0x55555555) + ((x>>1)&0x55555555)x = (x&0x55555555) + ((x>>1)&0x55555555)

x = (x&0x33333333) + ((x>>2)&0x33333333)x = (x&0x33333333) + ((x>>2)&0x33333333)

x = (x&0x0F0F0F0F) + ((x>>4)&0x0F0F0F0F)x = (x&0x0F0F0F0F) + ((x>>4)&0x0F0F0F0F)

x = (x&0x00FF00FF) + ((x>>8)&0x00FF00FF)x = (x&0x00FF00FF) + ((x>>8)&0x00FF00FF)

x = (x&0x0000FFFF) + ((x>>16)&0x0000FFFF)x = (x&0x0000FFFF) + ((x>>16)&0x0000FFFF)

return x;return x;

Page 77: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

ReferenceReference

Hackers DelightHackers Delight

Page 78: Counting Bits

www.gowrikumar.comwww.gowrikumar.com

The EndThe End