Splitting up bytes question


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Hi,
    Yes, it should of course be Number_2 = FullByte & %00001111, typo on my behalf, sorry about that.

    If you do Number_1 = FullByte & %11110000 you'll mask off the lower four bits (make them zeros) and assign the result to Number_1. In this case, when FullByte is 178 (10110010), you'd end up with 10110000 which 176 and not 11 which is what you're looking for.

    /Henrik.

  2. #2
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    So, with the shift (>>4), you'd go from 11110101 to 00001111 (for 15). But with & %11110000, you'd get 11110000 for Number_1.

    I thought %11110000 meant it would discard bits 0-3 and just enter 1111 into the Number_1 value. Apparently that's not the case.

    Tony

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Hi Tony,
    Yes, the & operator is bitwise AND.

    As input it takes two numbers, it then compares them bit by bit. Think of it like having running each bit in both numbers to a two-input AND-gate, (8 gates, one for each bit) the output of each gate is wired into the result variable. Just as with a real AND-gate both inputs (the same bit in the two numbers) have to set in order for the output (the bit in the result) to be set.

    Result = %10101010 & %11111111
    Here Result be a copy of the first number because all bits in the second number is set. Any bit which is SET in both numbers will be set in the result.

    Result = %10101010 & %0000000
    Here Result will be all zeros because the second number is all zeros.

    Result = %10101010 & %11110000

    Here Result will be %10100000 be because only the four bits of the second number are set.

    Result = %10101010 & %00111100
    Here Result will be 00101000 because, well I suppose you get it by now ;-)

    /Henrik.

  4. #4
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Makes total sense. Thank you so much guys for your help. While the book is helpful, it doesn't always break things down on a lower level for all people to understand. That's why these boards are so helpful, you can always find someone able to better present the information. Thanks again.

    Tony

Similar Threads

  1. splitting output from a counter
    By astouffer in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th September 2009, 21:21
  2. Please help: Splitting HROUT
    By luminas in forum Serial
    Replies: 9
    Last Post: - 1st June 2008, 14:52
  3. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 06:40
  4. Splitting a byte?
    By Deadeye in forum General
    Replies: 3
    Last Post: - 29th May 2005, 21:58

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts