Shop OBEX P1 Docs P2 Docs Learn Events
Byte variable and buffer question — Parallax Forums

Byte variable and buffer question

Don MDon M Posts: 1,652
edited 2012-08-30 15:24 in Propeller 1
Lets say I have this:
var  byte  Inventory, Items[16]


Where Inventory contains the quantity in bits 7-4 and the item number in bits 3-0. The Items buffer just contains the quantities (from 00 to FF) of 16 different items designated by byte position 0 as item 1, byte position 1 as item 2, etc...

If I receive an Inventory byte value such as $13 which would represent a quantity of 1 (0001) of item number 3 (0011) how do I parse or breakout the quantity bits and the item number bits so that I can adjust the item number's quantity in the Items buffer?

I know that I would want to do something like this:
a := byte[@Items][?] (? = the item number from the item bits of Inventory) ' to get the quantity of ? item number
a := a + ? (? = the quantity bits from Inventory)  ' to add to the existing quantity number
byte[@Items][?] := a ' to store the new quantity number of ? item number


I don't know how to break down the byte into bits and get what I need.

Thanks.
Don

Comments

  • JonnyMacJonnyMac Posts: 9,108
    edited 2012-08-30 14:48
    qty  := byte[@Items][idx] >> 4
    item := byte[@Items][idx] & $0F
    


    ...where idx is the index of the item you want to examine.
  • Don MDon M Posts: 1,652
    edited 2012-08-30 15:01
    Thanks Jon.

    Here is what I got out of your suggestion and it works the way I need it too.
    qty  := Inventory >> 4    ' strips out quantity portion of byte
    item := Inventory & $0F  ' strips out item number of byte
    
    

    Thanks!
    Don
  • RaymanRayman Posts: 14,671
    edited 2012-08-30 15:04
    One trick is to use the INB, OUTB, or DIRB registers to access individual bits...
  • Don MDon M Posts: 1,652
    edited 2012-08-30 15:13
    What I was looking for was to look at the 4 high bits and 4 low bits each as a group and figure out the hex (or decimal) value of each group.
  • RaymanRayman Posts: 14,671
    edited 2012-08-30 15:24
    You can do things like:

    INB:=items[x]
    highbits:=INB[31..28]
    lowbits:=INB[3..0]
Sign In or Register to comment.