PDA

View Full Version : Bit manipulation???



James Long
10-20-2006, 11:38 AM
Ok guys....have a question....not really difficult...but I would like to know.

Is it possible to manipulate one bit within the propeller? Of the variable type.....not on the registers or pins.

Please bear with me....I'm a ladder logic person....not really a written code type.

I would like to use bits as variables/flags....but I don't see a way to use just one bit. Using a byte is just huge overhead.

Thanks,

James L

Mike Green
10-20-2006, 11:52 AM
All bit manipulation on the propeller is done with 32 bit integer logical operations (&, |, ^, !) including the many different kinds of shift operations. For using individual bits as flags, use a mask like $00400000, preferably as a named constant (for clarity) and use the bit logical operations so to set the bit do "x |= $00400000", to clear the bit do "x &= !$00400000", to test if set do "(x & $00400000) <> 0", and to test if clear do "(x & $00400000) == 0". If you're careful, you can do combinations of bits the same way.

cgracey
10-20-2006, 12:17 PM
If you have over 32ˇˇbits, youˇcould make an arrayˇof longs with routines to read and write by bit number:

VAR long Bits[32] '1024 bits

PRI ReadBit(BitNum) : Bit
ˇ Bit := Bits[BitNum >> 5] >> BitNum & 1

PRI WriteBit(BitNum, State)
ˇ Bits[BitNum >> 5] |=ˇ|< BitNum
ˇ Bits[BitNum >> 5] ^=ˇ|< BitNum &ˇnotˇStateˇ

If you have only 32 bits, you could use the above code, but get rid of the [BitNum >> 5] indexes:

VAR long Bits '32 bits
ˇ
PRI ReadBit(BitNum) : Bit
ˇ Bit := Bits >> BitNum & 1
ˇ
PRI WriteBit(BitNum, State)
ˇ Bits |=ˇ|< BitNum
ˇ Bits ^=ˇ|< BitNum &ˇnotˇStateˇ
ˇ


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Chip Gracey
Parallax, Inc.

James Long
10-21-2006, 12:15 AM
Mike said...
All bit manipulation on the propeller is done with 32 bit integer logical operations (&, |, ^, !) including the many different kinds of shift operations. For using individual bits as flags, use a mask like $00400000, preferably as a named constant (for clarity) and use the bit logical operations so to set the bit do "x |= $00400000", to clear the bit do "x &= !$00400000", to test if set do "(x & $00400000) <> 0", and to test if clear do "(x & $00400000) == 0". If you're careful, you can do combinations of bits the same way.
Mike....the mask has me confused. Setting them.....Ok.....I get that part. I was going to doˇ combinations.....that way I could have 32 flags in one group.


Chip said...
If you have over 32ˇˇbits, youˇcould make an arrayˇof longs with routines to read and write by bit number:

VAR long Bits[32] '1024 bits

PRI ReadBit(BitNum) : Bit
ˇ Bit := Bits[BitNum >> 5] >> BitNum & 1

PRI WriteBit(BitNum, State)
ˇ Bits[BitNum >> 5] |=ˇ|< BitNum
ˇ Bits[BitNum >> 5] ^=ˇ|< BitNum &ˇnotˇStateˇ

If you have only 32 bits, you could use the above code, but get rid of the [BitNum >> 5] indexes:

VAR long Bits '32 bits
ˇ
PRI ReadBit(BitNum) : Bit
ˇ Bit := Bits >> BitNum & 1
ˇ
PRI WriteBit(BitNum, State)
ˇ Bits |=ˇ|< BitNum
ˇ Bits ^=ˇ|< BitNum &ˇnotˇStateˇ
ˇ

Chip I know I could have over 32 bit as flags.....but really doubtful. I use them alot ...but not in huge amounts.

I'll have to read up on your method of affecting them.....but I think I get it.

James

Mike Green
10-21-2006, 12:28 AM
James,
If you'd prefer using a bit number, you can use the "|<" operator. Setting Bit 22 is "x |= |<22". Clearing Bit 22 is "x &= !|<22". Testing Bit 22 to see if it's set is "(x & |<22) <> 0". Toggling Bit 22 is "x ^= |<22" and so on.
Mike

Newzed
10-21-2006, 12:44 AM
Mike, if you wanted to toggle a bit, couldn't you write:

x := !|<22

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
ˇ

cgracey
10-21-2006, 02:17 AM
You could even use some innocuous register like VSCL as a bit-addressable flag buffer:

Bit := VSCL[BitNum]ˇˇˇˇ 'read a bit
VSCL[BitNum] := Bitˇˇˇˇ 'write a bit
!VSCL[BitNum]ˇˇˇˇˇˇˇˇˇˇˇ'toggle a bit

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Chip Gracey
Parallax, Inc.

Post Edited (Chip Gracey (Parallax)) : 10/20/2006 6:22:52 PM GMT

James Long
10-21-2006, 02:39 AM
Chip,

Now that was very helpful.......Thanks.



Mike,

Thanks for the suggestion.



James L

Mike Green
10-25-2006, 12:32 AM
Sid,
"x := !|<22" doesn't mean what you think it does. The "!" is a logical "NOT" operator so "|<22" is $00400000 and "!|<22" is $FFBFFFFF, so your statement sets x to $FFBFFFFF.
Mike

cgracey
10-25-2006, 01:32 AM
Mike,
You're right. It would need to be:
x ^= |<22ˇˇˇˇˇˇˇˇ 'xor $00400000 into x


Mike Green said...
Sid,
"x := !|<22" doesn't mean what you think it does. The "!" is a logical "NOT" operator so "|<22" is $00400000 and "!|<22" is $FFBFFFFF, so your statement sets x to $FFBFFFFF.
Mike

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Chip Gracey
Parallax, Inc.