Shop OBEX P1 Docs P2 Docs Learn Events
Can't read HIGHBIT or LOWBIT of NIBble. Anyone knows how to do? — Parallax Forums

Can't read HIGHBIT or LOWBIT of NIBble. Anyone knows how to do?

FlotulFlotul Posts: 24
edited 2006-03-20 19:38 in General Discussion
Hello,

To get more of the BS2's memory, I want to adress, one by one, every pair (4 in total as you know) of each byte.

I can't read either the HIGHBIT neither the LOWBIT of each NIBble. Here's the code. What's wrong with it?

' {$STAMP BS2}
' {$PBASIC 2.5}

ValeurMem VAR Byte

DATA %10110011

Main:
READ 0, ValeurMem

DEBUG BIN4 ? ValeurMem.NIB0 'shows the four last bits (LOWNIB) of the byte - ok
DEBUG BIN4 ? ValeurMem.NIB1 'shows the four first bits (HIGHNIB) of the byte - ok
DEBUG BIN2 ? ValeurMem.NIB0.BIT0 'should show the two last bits of the lowest nibble - but it doesn't...
DEBUG BIN2 ? ValeurMem.NIB0.BIT1 'should show the two first bits of the lowest nibble - but it doesn't...
DEBUG BIN2 ? ValeurMem.NIB1.BIT0 'should show the two last bits of the highest nibble - but it doesn't...
DEBUG BIN2 ? ValeurMem.NIB1.BIT1 'should show the two first bits of the highest nibble - but it doesn't...

END

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Roger

Savigny, SWITZERLAND (french speaking part)

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-03-18 23:45
    See if something like this will work for you...


     DEBUG BIN4 ? ValeurMem.NIB0 'shows the four last bits (LOWNIB) of the byte - ok
    DEBUG BIN4 ? ValeurMem.NIB1 'shows the four first bits (HIGHNIB) of the byte - ok
    DEBUG BIN2 ? ValeurMem.BIT0 
    DEBUG BIN2 ? ValeurMem.BIT1
    DEBUG BIN2 ? ValeurMem.BIT4
    DEBUG BIN2 ? ValeurMem.BIT5 
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • FlotulFlotul Posts: 24
    edited 2006-03-18 23:58
    Yes, it works but doesn't help me because I can't addition the two bits and get a hex result. The last two bits of a byte should make "3".

    I made this same exercise one hour ago but one level "byte level". Here, it works. But since I have a need for only 4 (0 to 3) results (or possibilities), I want to make the same at nibble level.

    Can you help? Following your suggestion, would itbe possible to addition those 2 bits? How?

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ValeurMem VAR Byte

    DATA %00010011

    Main:
    READ 0, ValeurMem

    DEBUG BIN4 ? ValeurMem.LOWNIB, BIN4 ? ValeurMem.HIGHNIB
    DEBUG ? ValeurMem.LOWNIB, ? ValeurMem.HIGHNIB

    END

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Roger

    Savigny, SWITZERLAND (french speaking part)
  • CJCJ Posts: 470
    edited 2006-03-19 00:06
    nibbles are 4 bits each, I have not seen any built in way to access bits in pairs like you want

    byte: 10100111
    nibbles: 1010 0111
    pairs: 10 10 01 11

    ^ my assumption of how you want to break it down

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • FlotulFlotul Posts: 24
    edited 2006-03-19 00:35
    Yes, that's it. I just don't understand why it works on byte level and not on nibble level.

    What is even stranger to me is that I could find the LOWNIB (NIB0) or HIGHNIB (NIB1) arguments in the help file under "Memory and Variables" ...

    Is it really not possible?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Roger

    Savigny, SWITZERLAND (french speaking part)
  • CJCJ Posts: 470
    edited 2006-03-19 00:41
    why not just declare variables as nibbles?

    myVar VAR Nib

    anyways, the bit modifiers only give you access to one bit at a time

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-03-19 05:25
    Roger,

    What you can do is create a mask within each nibble %0011 or %1100 and shift right by 2 ">>2"
    if you want to access the upper 2 bits of the nibble. This way, if you want you can assign a seperate
    variable to the 2-bits that you are interested in. Hope this helps.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ValeurMem VAR Byte
    
    Temp1     VAR Nib 'reserved for the two lower bits of the lower nibble of ValeurMem
    Temp2     VAR Nib 'reserved for the two upper bits of the lower nibble of ValeurMem
    Temp3     VAR Nib 'reserved for the two lower bits of the upper nibble of ValeurMem
    Temp4     VAR Nib 'reserved for the two upper bits of the upper nibble of ValeurMem
    
    DATA %10110011
    
    Main:
    READ 0, ValeurMem
    
    Temp1 = ValeurMem.NIB0 & %0011
    Temp2 = (ValeurMem.NIB0 & %1100) >>2
    Temp3 = ValeurMem.NIB1 & %0011
    Temp4 = (ValeurMem.NIB1 & %1100) >>2
    
    DEBUG BIN4 ? ValeurMem.NIB0 'shows the four last bits (LOWNIB) of the byte - ok
    DEBUG BIN4 ? ValeurMem.NIB1 'shows the four first bits (HIGHNIB) of the byte - ok
    DEBUG BIN2 ? ValeurMem.NIB0.BIT0 'should show the two last bits of the lowest nibble - but it doesn't...
    DEBUG BIN2 ? ValeurMem.NIB0.BIT1 'should show the two first bits of the lowest nibble - but it doesn't...
    DEBUG BIN2 ? ValeurMem.NIB1.BIT0 'should show the two last bits of the highest nibble - but it doesn't...
    DEBUG BIN2 ? ValeurMem.NIB1.BIT1 'should show the two first bits of the highest nibble - but it doesn't...
    
    DEBUG CR
    DEBUG BIN4 ? ValeurMem.NIB0                 'shows the four last bits (LOWNIB) of the byte - ok
    DEBUG BIN4 ? ValeurMem.NIB1                 'shows the four first bits (HIGHNIB) of the byte - ok
    DEBUG BIN2 ?  ValeurMem.NIB0 & %0011        'shows the two last bits of the lowest nibble
    DEBUG BIN2 ? (ValeurMem.NIB0 & %1100) >>2   'shows the two first bits of the lowest nibble
    DEBUG BIN2 ?  ValeurMem.NIB1 & %0011        'shows the two last bits of the highest nibble
    DEBUG BIN2 ? (ValeurMem.NIB1 & %1100) >>2   'shows the two first bits of the highest nibble
    
    DEBUG CR
    DEBUG DEC ? Temp1                           'shows the decimal value of the last two bits of the lowest nibble
    DEBUG DEC ? Temp2                           'shows the decimal value of the first two bits of the lowest nibble
    DEBUG DEC ? Temp3                           'shows the decimal value of the last two bits of the highest nibble
    DEBUG DEC ? Temp4                           'shows the decimal value of the first two bits of the highest nibble
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • FlotulFlotul Posts: 24
    edited 2006-03-19 07:22
    Hello Beau,

    THANK YOU smile.gif

    This is exactly what I needed.

    I have never been dealing with the binary operators before so I didn't even know where to look to answer my question.

    Again, Beau, thanks a lot. I really appreciate the time you must have spent on my problem.

    Thank you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Roger

    Savigny, SWITZERLAND (french speaking part)
  • CJCJ Posts: 470
    edited 2006-03-19 14:19
    slick!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-03-19 14:34
    Roger,

    You can also do something like this to go the other direction...



    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ValeurMem VAR Byte
    
    Temp1     VAR Nib 'reserved for the two lower bits of the lower nibble of ValeurMem
    Temp2     VAR Nib 'reserved for the two upper bits of the lower nibble of ValeurMem
    Temp3     VAR Nib 'reserved for the two lower bits of the upper nibble of ValeurMem
    Temp4     VAR Nib 'reserved for the two upper bits of the upper nibble of ValeurMem
    
    Temp1 = 3   'or  Temp1 = %11
    Temp2 = 0   'or  Temp1 = %00
    Temp3 = 3   'or  Temp1 = %11
    Temp4 = 2   'or  Temp1 = %10
    
    ValeurMem = Temp4
    ValeurMem = (ValeurMem << 2) + Temp3
    ValeurMem = (ValeurMem << 2) + Temp2
    ValeurMem = (ValeurMem << 2) + Temp1
    
    DEBUG BIN4 ? ValeurMem.NIB0 'shows the four last bits (LOWNIB) of the byte
    DEBUG BIN4 ? ValeurMem.NIB1 'shows the four first bits (HIGHNIB) of the byte
    
    
    
    
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-19 15:54
    Here's another example, albeit a bit simpler; the BIN modifier truncates unused bits for us, so we can eliminate some masking:

    '·{$STAMP BS2}
    ' {$PBASIC 2.5}

    temp··· VAR···· Byte

    Main:
    · temp = $A5

    · DEBUG BIN8 temp, CR
    · DEBUG REP " "\0, BIN2 temp.NIB1 >> 2, CR
    · DEBUG REP " "\2, BIN2 temp.NIB1, CR
    · DEBUG REP " "\4, BIN2 temp.NIB0 >> 2, CR
    · DEBUG REP " "\6, BIN2 temp.NIB0, CR

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • FlotulFlotul Posts: 24
    edited 2006-03-20 19:38
    Hi Jon,

    Although your solution is more simple, it doesn't work as I need it. Have a look to what happens when you replace the "BIN2" argument by "BIN".

    I'll stick to Beau's solution. In his case, I get for each pair of bits the decimal value calculated as if the byte would only be 2 bits long.

    Example: %11100100 ($E4)

    After the byte has been decomposed, the decimal values are:
    - for bits 2 and 1 = 0
    - for bits 4 and 3 = 1
    - for bits 6 and 5 = 2
    - for bits 8 and 7 = 3

    Using caracters (elements) that have only 3 different possible values, I can now store FOUR TIMES MORE datas in the data memory of my BS2 (Bytes X 4)!

    Isn't this great?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Roger

    Savigny, SWITZERLAND (french speaking part)
Sign In or Register to comment.