math - rounding to the nearest zero, bitwise -


i wonder how can round nearest 0 bitwise? previously, perform long division using loop. however, since number divided number power 2. decide use bit shifting. so, can result this:

12/4=3 13/4=3 14/4=3 15/4=3 16/4=4 

can performing long division usual?

12>>2 13>>2 

if use kind of bit shifting, behavior different different compiler? how rounding up? using visual c++ 2010 compiler , gcc. thx

bitwise shifts equivalent round-to-negative-infinity divisions powers of two, meaning answer never bigger unrounded value (so e.g. (-3) >> 1 equal -2).

for non-negative integers, equivalent round-to-zero.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -