0

I am trying out some program for my application which will continue scan for user input. There are two numbers say num1 and num2 both of 8 bits long and num1 will always hold some value.

Now whatever value user sets in num2 should get added in num1 without altering its previous value.

In simple nth value of num2 should get added with nth value of num1.

Example:

num1                      0000 0100
num2                      0010 0101 

Updated value of  num1    0010 0101

Can someone help me out how to perform bitwise operations for same?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 6
    It is not quite clear what "*should get added in num1 without altering its previous value*" means, but sounds like you are looking for bitwise OR operation (`|`) – Eugene Sh. May 05 '22 at 15:32
  • What do you mean by "without altering its previous value"? Also, from your example it looks like you just want to copy num2 to num1? – Siguza May 05 '22 at 15:32
  • Does this answer your question? [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – phuclv May 05 '22 at 15:34
  • Try to take a look [here](https://www.tutorialspoint.com/bitwise-recursive-addition-of-two-integers-in-c). – overmach May 05 '22 at 15:35

1 Answers1

1

I believe you want:

unsigned char num1 = 0b00000100;  // 0x04
unsigned char num2 = 0b00100101;  // 0x25
num1 |= num2;                     // Now num1 = 0b00100101; or 0x25

printf("0x%X\n", num1);           // Expect output of 0x25
abelenky
  • 63,815
  • 23
  • 109
  • 159