0

Say we have 11101111 stored in the address address, how would I add the first 4 bits to the last 4 bits?

My prof showed us we can do this (*address)/16 + (*address)%16 but I don't understand why it works. Can someone explain where the division and modulo with 16 come from?

10
  • 2^4 = 16 - dividing by 16 is equivalent to shifting off the last four bits. Commented Nov 24, 2016 at 21:04
  • Compute the binary results of *a/16 and *a%16. Commented Nov 24, 2016 at 21:06
  • You should learn a bit more about binary and numerical bases in general Commented Nov 24, 2016 at 21:06
  • 1
    What do you propose to do with any carry bit? Commented Nov 24, 2016 at 21:07
  • I learned about division and remainder at elementary school. Commented Nov 24, 2016 at 21:09

1 Answer 1

1

@VanGo, See to perform operation on bits, you have to learn bitwise operator first.

I am explaining your problem here.

11101111 (is in binary form) and is equivalent to 239 (in decimal). Now you have to add 1110 in 1111. In order to get these two pair of 4 bits from 11101111, you have to perform bitwise operation on 11101111.

To get higher 4 bits, shift 11101111 four times from left to right.

*address >> 4 :- is equal to *address/16

internally compiler convert *address>>4 into (*address)/(2 pow 4).

To get lower 4 bits, either perform (*address)&0x0f or (*address)%16. Both operation will clear all bits except lower 4 bits.

 printf(".....%d\n",(((*address)>>4) + ((*address)&0x0f))); 

hope it helps you.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.