Bit manipulation
Reset_Vector
Posts: 55
Hi all propeller's guys !
I wonder if there is an easy way to manipulate bits into a byte ,a word or a long, set or clear a bit and test the bit state
eg, to manipulate a particular bit :
setbit(6,variable) or clearbit(8,variable) where the literal is the bit number into variable .
or to test the state of a particular bit :
if bit(0,variable) == true .....
Thank you !!!!
I wonder if there is an easy way to manipulate bits into a byte ,a word or a long, set or clear a bit and test the bit state
eg, to manipulate a particular bit :
setbit(6,variable) or clearbit(8,variable) where the literal is the bit number into variable .
or to test the state of a particular bit :
if bit(0,variable) == true .....
Thank you !!!!
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
Just do
By the way ... it's not really useful to have functions for that at all. It increases the number of longs you need for the stack, it increases the runtime and possibly it also increases the memory.
Simply do the following
You have to replace bit by the number of the bit. Constant will then generate the right bitmask during compile-time. If the bit number can change during runtime, then remove the constant. That increases runtime.
if bit(0,variable) == true
true would not = 8 or 64 or whatever
I too would inline it, ( if I wasn't doing it in PASM ) etc, but he was wanting them as functions afaik [noparse]:D[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
The more experienced posters should instead tell, in case they have better knowledge, how to write 'better' code. Sometimes it even means to give several possibilities when different optimisations (e.g. for code size of for runtime) result in different code.
Ok .. got me with the == true. Did not take that into account. But still there is a solution without if and 2 returns:
return not( long[noparse][[/noparse] varptr ] & (|<bit) == 0 )
The reason I try not to make the code too intricate, is because I don't want to over complicae things, he only has 10 posts, so I don't know his level of coding skill, so didn't want to write some intricate deep inline code that might have been over his head. [noparse]:D[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
Why not just
return long[noparse][[/noparse] varptr ] & (|<bit) <> 0
?
a) I only wanted to leave some room for improvements
b) Operator blindness?
c) makes the code to fast
Choose by yourself ;o)
@baggers:
I prefer telling the newbies the whole truth right from the beginning. Otherwise unfortunately coding can become a habit. We talk about a microcontroller and not about a PC with lots of GB. Each byte that you don't use might be valuable in a bigger project.
But guys ! I don't want do open a fight between the propellers coders !
So, I apreciate the differents ways you tell me to do what I need.
If I tell you that my project is to implement a programmable logic controller in a Propeller, I think i'll have a lot of replies !?
And if I tell you that this project comes from a previous one I did (and which works !) with a Microchip PIC18 !!!, what still
happens !!!!
Best regards
But that does not matter! Whatever questions pop in in this forum, you have a good chance to get an answer, because you find a lot of friendly people over here! And I don't see a fight here in your thread. I only wanted to point out, that I prefere to not only give the solutions, but also like to argue why this solution is good in this case or maybe give the original post a different direction.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·