Thread: Bytes

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    7

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    7
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that's because your expression is wrong. Why did you left shift both of them by 8?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    7
    That is simple, both b1 and b2 are 8 bits numbers..

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24
    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.
    Last edited by TheRaven; 03-18-2019 at 11:25 PM. Reason: Grammar, refinements

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    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 subscribe to a feed

Similar Threads

  1. how do I tell if there is bytes <4
    By Once-ler in forum C Programming
    Replies: 3
    Last Post: 11-24-2011, 07:07 PM
  2. how much bytes does an int take in c
    By tubby123 in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 12:34 PM
  3. working set, virtual bytes and private bytes
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2008, 02:39 AM
  4. Bytes
    By kris_perry2k in forum C Programming
    Replies: 10
    Last Post: 01-19-2006, 02:54 PM
  5. What can YOU do with 256 Bytes??
    By MrWizard in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-21-2002, 02:56 AM

Tags for this Thread