michael
michael

Reputation: 110520

What does this c code trying to do

What does the following c code trying to do. I am not sure what does it calculate the value of width to

(width+31)&~31

Thank you.

Upvotes: 1

Views: 86

Answers (3)

Bhavesh Munot
Bhavesh Munot

Reputation: 675

It resets (turns off) the last 5 bits of the 'width' which leads to width%32=0 i.e. width multiple of 32.

Upvotes: 0

Sneftel
Sneftel

Reputation: 41464

It rounds width upwards to a multiple of 32.

Upvotes: 3

Solomon Slow
Solomon Slow

Reputation: 27115

It is rounding up to the next multiple of 32. It only works because 32 is a power of 2.

The bit pattern for  31 is ...000000000011111
The bit pattern for ~31 is ...111111111100000

When you and ~31 with any positive integer, you get a multiple of 32 (five low order bits are all zeroes).

Upvotes: 4

Related Questions