1

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.

1
  • Too many right parentheses, but the obvious intent is correct (with the stipulation identified by EboMike that new_value < 256 :) Commented Aug 2, 2011 at 4:49

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

snap. And ensuring unsigned was something I forgot - good point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.