Accessing several bits together in a byte or word...


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76

    Default Accessing several bits together in a byte or word...

    I know there is a way... but can't find it in a search and can't remember how to do it...

    I would like to insert a three-bit number into a byte or word such as...

    Put 110 into byte xxxxxxx and get xxxx110x.

    I know how to access single bits, low and highbytes... etc. But I need to REPLACE several bits in a byte in one command.

    Thank you,

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rossfree View Post
    I know there is a way... but can't find it in a search and can't remember how to do it...
    I would like to insert a three-bit number into a byte or word such as...
    Put 110 into byte xxxxxxx and get xxxx110x.
    I know how to access single bits, low and highbytes... etc. But I need to REPLACE several bits in a byte in one command.
    Thank you,
    Ross
    oldval var byte
    newval var byte

    oldval = %01010101 'just an example value
    newval = %110 'but you don't want this into the lower 3 bits, you want it in bits2,3,4...
    newval = newval << 1 'shift the newval bits left one position
    oldval = oldval AND %11110001 'clear out old bits that you want to replace
    oldval = oldval | newval 'bitwise OR to set the bits

    It's not one command...I don't think you'll ever get away with trying to use one command. Too many situations, too many (pardon the pun) variables.

  3. #3
    Join Date
    Oct 2004
    Location
    New Hampshire
    Posts
    76


    Did you find this post helpful? Yes | No

    Default

    Yep... that should do it.

    Thank you Skimask

    Ross
    Never enough knowledge to be called intelligent but just enough knowledge to be considered dangerous!

    I like that! :-)

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 08:55
  2. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th July 2008, 00:19
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 19:04
  4. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 12:47
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 15:31

Members who have read this thread : 1

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