bit shifting

foTONICS

Sep 30, 2011
332
Joined
Sep 30, 2011
Messages
332
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:

pic16f627a
MPLAB X
xc8 compiler
pickit2
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
sPORTA.port << 1

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

sPORTA.port = sPORTA.port << 1

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.

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

That sounds appropriate.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
sPORTA.port << 1
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.
 
Top