bit wise manipulation on a 32-bit integer
I am trying to read开发者_StackOverflow a 32 register,modify its first 8 bits[BIT7:BIT0] and write back its value. Does the code below achieve that?
reg_val = register_read(register_object);
reg_val = ((reg_val & 0xffffff00) | new_value));
register_write(register_object,reg_val);
Also is it the most efficient way to achieve that.any suggestions or comments are appreciated.
Unless new_value
is guaranteed only 8 bits wide you should ensure it:
reg_val = ((reg_val & 0xffffff00) | (new_value & 0xff));
Also is it the most efficient way to achieve that?
Any compiler worth its salt will translate that into The Right Thing.
Assuming that new_value
is guaranteed to be <256, and assuming that these are unsigned integers, then yes, this is the proper way to do it.
精彩评论