简体   繁体   中英

bit wise manipulation on a 32-bit integer

I am trying to read 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.

假设保证new_value <256,并假定它们是无符号整数,则可以,这是正确的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM