Shop OBEX P1 Docs P2 Docs Learn Events
Need help with Subroutine — Parallax Forums

Need help with Subroutine

MovieMakerMovieMaker Posts: 502
edited 2009-05-13 15:21 in Robotics
OK, here is what I need to do.· I am not a mathmatician, but here goes:

I want to have a 16 bit binary number. I want to store it somewhere , maybe eprom.
I want to be able to read anyone of the 16 bits.
I want to be able to write to anyone of the 16 bits.
How would I MASK individual bits in order to do this?

I need to know how to isolate each individual bit.

I am not knowlegable in this area.

Thanks!



·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-13 14:39
    On a Stamp, the easiest thing to do is to use the ".BIT" suffix on a word variable. If you had a variable FOO, you'd access specific bits like: FOO.BIT5 or FOO.BIT13. You'd treat this just like any other variable. You can assign a 0 or 1 to it or you can test it in an IF statement, etc. Look in the PBasic Manual, page 90.

    You can also access these individual bits as if they were a bit array like: FOO.BIT0(i)
  • SRLMSRLM Posts: 5,045
    edited 2009-05-13 14:46
    To make a mask for accessing one bit, you can use either of the bitwise operators AND or OR depending on the operation.

    Say you want to access bit 6. You'll make a mask like the following:

    mask = %0000000001000000

    Where the bit that you want to access (6) is denoted by a 1 in the above mask. Then you AND that with your number:

    result = my_num & mask

    If result is zero, then then it's a 0 in that position, otherwise it's a 1.

    You'd use just the reverse to write to that position:

    mask = %1111111101111111
    my_num = my_num & mask

    Clears that bit

    mask = %0000000001000000
    my_num = my_num | mask

    Sets that bit high.
  • MovieMakerMovieMaker Posts: 502
    edited 2009-05-13 15:21
    Thanks Guys, You two are the Greatest!
Sign In or Register to comment.