gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > bit shifting question

#9842 - Lupin - Tue Aug 19, 2003 7:13 pm

im wondering how negative numbers are shifted, if bit 31 indicates an negative number and i shift the number right by 1 the number is no more negative, isnt it?

and if bit 30 is set and i shift it left does the number get negative then?

im a bit confused about that...

#9845 - col - Tue Aug 19, 2003 7:58 pm

Lupin wrote:
im wondering how negative numbers are shifted, if bit 31 indicates an negative number and i shift the number right by 1 the number is no more negative, isnt it?


There are two kinds of shift instruction on the ARM - logical shift and arithmetic shift.
arithmetic shift preserves the sign. your compiler should choose the correct instruction depending on whether the variable in question is a signed or an unsigned type

Lupin wrote:

and if bit 30 is set and i shift it left does the number get negative then?

im a bit confused about that...


if you are using an s32 with bit 30 set and you shift left, you should consider that as an overflow !
if the variable is a u32, its ok (unless you want to cast it to an s32 at some point)

cheers

Col