Splitting up bytes question


Closed Thread
Results 1 to 17 of 17

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Hi Tony,
    Once the value is written to EEPROM the only thing that will change that value is if you do another WRITE to the same location or erase the EEPROM. When you're going to get the value from the EEPROM you'll need to read the full BYTE into a variable and then split it
    Code:
    READ 0, FullByte    ' Get value from EEPROM and put it in FullByte
    Number_1 = FullByte >> 4
    Number_2 = FullByte %00001111
    The second line will not alter FullByte in any way, basically it'll take a copy of it, shift to the right four times and put the result in Number_1, leaving FullByte in its original state. Again, note that you aren't doing these operations directly on the EEPROM.

    Lets take the example with 11 and 2 which resulted in the value 178 being stored in EEPROM. When you read the EEPROM you'll get the value 178 into the variable FullByte. Then we take the value 178 (10110010 in binary) and shift that to the right four times, pushing out the lower four bits, filling up the top four bits with zeros, the result is stored as Number_1 (00001011, which is 11 in decimal). Then we go back to the original value (178) again and mask off the four high bits so they not end up in Number_2 which will then contain 00000010, which is 2 in decimal. Both the EEPROM and FullByte still contains the original value (178).

    Now you can manipulate either one of the values and the piece them back together as shown earlier. Obviously you need to do another WRITE to actually save the new value in EEPROM because again, you can't manipulate the values directly "in" the EEPROM.

    Another way of thinking of this, which is what the other guys here are saying is use hex notation. Remember now that the actual numbers does not change in any way, it's just how WE express them. 178 in decimal is B2 in hex right? B in hex is 11 in decimal and 2 in hex is 2 in decimal. It doesn't change anything, it's just another way of looking at it.

    If "all" you're doing is increasing or decreasing the individual numbers then you could do that directly on the full byte instead of splitting them and then putting them back together. If Number_1 (the value stored in the high four bits) should be increased then you add 16 to value. If the Number_2 (the value stored in the low four bits) should be increased you add 1. Let's do that:
    Code:
    Read 0, FullByte   ' Get original value from EEPROM
    FullByte = FullByte + 17
    WRITE 0, FullByte
    Now, the first we get the value 178 from EEPROM, then we add 17 resulting in 195 which in hex is C3. C in hex is 12 in decimal (one more than 11) and 3 in hex is 3 in decimal (one more than 2).

    /Henrik.

  2. #2
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Number_1 = FullByte >> 4

    I get that this means to take the value of Fullbyte, shift it to the right 4 times and place the value in Number_1.

    Number_2 = FullByte %00001111

    But what does this mean? Is there suppose to be an & between Fullbyte & %00001111? If so, then I completely understand.

    Why couldn't you do:
    Number_1 = FullByte & %11110000 with the first one? Less words the other way?

    Thanks,
    Tony

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


    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.

  4. #4
    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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,623


    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.

  6. #6
    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, 22:21
  2. Please help: Splitting HROUT
    By luminas in forum Serial
    Replies: 9
    Last Post: - 1st June 2008, 15:52
  3. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 07:40
  4. Splitting a byte?
    By Deadeye in forum General
    Replies: 3
    Last Post: - 29th May 2005, 22: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