How to do bitwise shift across several variables/array members?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default How to do bitwise shift across several variables/array members?

    Say there is an array, having 16 member, 1 byte each. I want to shift bits say to right in that way, that the byte that "leaves" say 1st member, gets shifted in into 2nd member, and so on (and bit shifted out from last member, shifts into 1st member). Are there any simple ways to do this, except that reading each member bit by bit, assembling in new variable and writing back?
    Last edited by CuriousOne; - 14th October 2021 at 09:40.

  2. #2
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?


  3. #3
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    So you want to say, I can shift left or right whole contents of an array?

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    Are you using the shiftout command? If so you can select MSB or LSB first.

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


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    No, there is no "one command way" to shift or rotate bits spanning multiple members of an an array. You will have to write some code to do that.

  6. #6
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    Start with the least significant byte, use the Shift-Left "<<", then check the STATUS Register Carry bit to see if you have to add one to the more significant byte after shifting.

  7. #7
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    I'm thinking it in the following way (as example, 2 byte array, shifted to right)

    1. Read both array bytes into variables A and B
    2. Shift B right 1 bit
    3. Add LSB from A as MSB to B
    4. Shift A right
    5. Write A and B back to array

    Repeat as desired.


    Correct?

  8. #8
    Join Date
    Aug 2011
    Posts
    408


    Did you find this post helpful? Yes | No

    Default Re: How to do bitwise shift across several variables/array members?

    Yes, assuming you really do want a shift and don't want to rotate the value shifted out of B back into the MSB of the array.
    There's no need to copy the array bytes to A and B, but if it helps you keep it straight then go for it.

Similar Threads

  1. SWAP COMMAND extended to Longs and array variables
    By longpole001 in forum PBP Wish List
    Replies: 1
    Last Post: - 10th April 2018, 00:41
  2. MIBAM and Array Variables in FOR-NEXT Loops
    By Homerclese in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th August 2010, 13:50
  3. Seeting array variables in For-Next loop
    By bcd in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th October 2007, 08:01
  4. Reading Array values into variables
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 21st March 2005, 11:30
  5. List of Members
    By NavMicroSystems in forum Forum Requests
    Replies: 5
    Last Post: - 17th January 2005, 18:10

Members who have read this thread : 2

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