1. ## Bytes

Hello, can anyone give me any hint? I want to create a function which creates a number (16 bits) from 2 bytes - b1 and b2 (b1 means first 8 bits, b2 means last 8 bits. All I have so far is converting b1 and b2 to 10. number, and it sums 2 integers, what is obviously wrong.

Thank you. 2. Use bitwise operations, e.g., << and |

Alternatively, you can use multiplication and addition, but then as you're explicitly working with bits and bytes the bitwise operations may convey your intent better. 3. Well, I tried to make it like this:

int byte = (b1 << 8) | (b2 << 8);
return byte;

.. but it seems like the final number (byte) is wrong. 4. Yes, that's because your expression is wrong. Why did you left shift both of them by 8? 5. That is simple, both b1 and b2 are 8 bits numbers.. 6. Originally Posted by dan789
That is simple, both b1 and b2 are 8 bits numbers..
That answer actually indicates that despite saying "that is simple", you haven't really thought this through.

Let's see what you're doing. Suppose b1 = 1 and b2 = 2:
b1 = 0000 0001
b2 = 0000 0010

Now, we left shift both by 8:
b1 = 0000 0001 0000 0000
b2 = 0000 0010 0000 0000
Then we bitwise or:
r1 = 0000 0011 0000 0000

What we want is:
r1 = 0000 0001 0000 0010

Can you see your mistake now?

If you cannot see it, then explain to me why do you left shift instead of right shift, and why do you use bitwise or instead of bitwise and. 7. Just a quick FYI Dan789 --do be aware that when performing pseudo addition with logical bitwise OR operations the carry flag (eflags/rflags [32 & 64 bit respectively]) is not going to be set; might be the issue you're addressing. Laserlight is definitely correct when suggesting the use of addition normally unless you intend on setting the carry flag yourself O.O

10 | 10 will resolve to 10 (1010 | 1010 returns 1010)
while sum of 10 and 10 resolves predictably (20) because the CPU uses a register to track a carry of 1 using a flag register.
(further elaborating/illustrating what Laserlight stated)

Now, if B1 & b2 both equal 10 then:
(b1 << 8) | (b2 << 8) b1 = b2 = 2560 because bitwise Logical OR compares both b1 & b2 bitwise; 1 & 1 = 1, 1 & 0 = 1 and 0 & 0 = 0 (binary bits). Both b1 and b2 are identical thus the return value is identical to the values expressed by both b1 & b2.

b1 = 101000000000
b2 = 101000000000

byte will also equal 101000000000 after the bitwise Logical OR operation. If you are getting 2560 then there is no programmatic or compiler errors. If you are expecting double the value remember what was said about using addition understanding (additionally) what I had stated about the carry flag not being set. Addition (add [CPU] operation) uses the carry flag, but the bitwise logical OR (or [CPU] operation) does not. 8. TheRaven: the carry flag has no relevance to this problem. C has no direct access to the carry flag; besides that, some CPUs don't even have a carry flag, or even any status flags at all. Popular pages Recent additions bits, bytes, converting, means, number 