Connect with us

bit shifting

Discussion in 'Microcontrollers, Programming and IoT' started by foTONICS, Jan 11, 2014.

Scroll to continue with content
  1. foTONICS


    Sep 30, 2011
    so I know that: 6 << 1 will shift 0110 to become 1100.

    so I have this variable "sPORTA.port" that I will load with data, shift it, then store the data into PORTA. So I want to shift bits one at a time so I tried:

    sPORTA.port << 1

    but that doesn't do anything, so I tried:

    sPORTA.port = sPORTA.port << 1

    and that gets the job done, 0001 becomes 0010 which then becomes 0100 and so forth and so forth. Is there a simpler way to do this, am I doing this a real backwards way?

    The reason I'm using a union variable is because I want to be able to write some data to the entire variable then be able to pick individual bits out, any suggestions?

    my setup:

    xc8 compiler
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    Jan 21, 2010
    This left shifts the bits, but doesn't place them anywhere.

    This left shifts the bits and applies the result back to the original variable.

    This sounds like what you want to do, and it is an appropriate way to do it.

    That sounds appropriate.
  3. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    Nov 28, 2011
    That doesn't do anything and will probably be optimised into nothing by the compiler. It's an EXPRESSION that evaluates to sPORTA.port shifted left by one bit. You have to assign it to something if you want to use it.

    What you want is sPORTA.port <<= 1.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day