byte and one bit in the byte
Zap-o
Posts: 452
I am wondering how to go about this. Say I have a byte "Numb" and I want to manipulate bit 6 in the byte.
for example:
Numb := %1111_0000
Now I want to make all the bits in Numb the same except bit 6.
Numb := %1011_0000
I would prefer to have 2 variables i.e. Numb and the NewNumb
NewNumb := %1
Numb := %11newNumb1_0000
I hope this makes since.
for example:
Numb := %1111_0000
Now I want to make all the bits in Numb the same except bit 6.
Numb := %1011_0000
I would prefer to have 2 variables i.e. Numb and the NewNumb
NewNumb := %1
Numb := %11newNumb1_0000
I hope this makes since.
Comments
EDIT: SORRY THIS IS GARBAGE FOR BYTES
Post Edited (Graham Stabler) : 2/10/2010 3:02:15 PM GMT
Bit indexing like that only works on SFR's.. If Numb was defined as a byte, that would write the byte at @Numb+6 instead.
Off the top of my head.. something like..
Numb := (Numb & !(1<<6)) | (NewNumb <<6)
.. might work
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
Post Edited (BradC) : 2/10/2010 1:56:28 PM GMT
Turn bit 6 on: numb |= |< 6
"numb or-equals bitwise-decode six"
Turn bit 6 off: numb &= ! |< 6
"numb and-equals bitwise-not bitwise-decode six"
I cant understand that stuff you put down localroger. Please give me a few more minutes and write down a little more details. I am even reading the propeller manual (bitwise decode) and I am lost. I guess its got something to do with me going to work at 2am.
It's really easy once you get your head around it..
(|< 6) would give you the same result of (1 << 6) but uses less code space and does it quicker.
In plain english, you give it a number between 0 & 31, and it gives you a long with only that particular bit set.
So to set the bit in a long, you would use xx |= |< bit
|< bit generates a long with only the single bit set, and or's it with the existing value (setting that bit unconditionally)
To unset the bit you would use xx &= ! |< bit
the ! |< bit generates the long with the single bit set, and the ! inverts it to create a long with all bits except that one set. If you and that with the original value, you get the original value without that bit set.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
newVal := old Val "bitwise or" bit-pattern
You get the bit-pattern of a single bit by using |<x operation, where x is the number of the bit you want to set.
If you want to clear a bit, then you can use "bitwise and" where all bits are 1 except the one you want to clear.
new Val := old Val "bitwise and" with bit-pattern where bit to clear is 0
This bit-pattern can be generated by inverting the |<x, which is !|<x
localroger simply used the shortcut of assigning operators, which do the bitwise or/and of left and righthand operand and directly assign the result back to the lefthand-operand.
(|= and &=)
PS: I should switch off all interrupts, when answering posts ;o)
Post Edited (MagIO2) : 2/10/2010 2:36:22 PM GMT
No a byte is fine.
All operations in the spin interpreter are carried out as long. If you declare a byte, when the operation is written out, it is simply truncated down to the declared size.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
This works because I can set any bit at any time then send it out to the serial to parallel IC.
A trivial change, based on localrogers concept above :
That would be marginally faster an a poofteenth smaller.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.