Shop OBEX P1 Docs P2 Docs Learn Events
Bit manipulation — Parallax Forums

Bit manipulation

Reset_VectorReset_Vector Posts: 55
edited 2009-04-10 14:05 in Propeller 1
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 !!!!

Comments

  • BaggersBaggers Posts: 3,019
    edited 2009-04-09 14:20
    Try something like this.
    'example code
    var
    long variable1
    long variable2      
    
    pub test
      if Test_Bit_Long( 0 , @variable1 )
        Clear_Bit_Long( 0 , @variable2 )
      else
        Set_Bit_Long( 0 , @variable2 )
    
    pub Test_Bit_Long( bit , varptr )
      if long[noparse][[/noparse]varptr] & (|<bit)
        return true
      return false
    
    pub Set_Bit_Long( bit , varptr )
      long[noparse][[/noparse]varptr] |= |<bit
    
    pub Clear_Bit_Long( bit , varptr )
      long[noparse][[/noparse]varptr] &= -1 - ( |<bit )
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-09 17:58
    Why having an if and two different returns?

    Just do

    pub Test_Bit_Long( bit, varptr )
       return long[noparse][[/noparse] varptr ] & (|<bit)
    
    

    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
    variable |= constant( |<bit ) ' to set a bit
    variable &= constant( ! |<bit ) ' to clear a bit
    
     
    variable & constant( |<bit ) in an if statement to test for a bit
    
    

    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.
  • BaggersBaggers Posts: 3,019
    edited 2009-04-09 18:27
    the reason for the if and two returns, was because he had

    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

    ·
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-09 19:01
    You should not always give the original poster what he asks for ;o)
    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 )
  • BaggersBaggers Posts: 3,019
    edited 2009-04-09 19:09
    Yes, very true. and nice piece of spin. [noparse]:D[/noparse]

    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

    ·
  • mparkmpark Posts: 1,305
    edited 2009-04-09 20:30
    MagIO2,
    Why not just
    return long[noparse][[/noparse] varptr ] & (|<bit) <> 0
    ?
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-09 20:42
    @mpark:
    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.
  • Reset_VectorReset_Vector Posts: 55
    edited 2009-04-10 08:44
    Thank you to alls,

    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
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-10 10:50
    What happens ? ... Well, I now wonder how you managed to program a logic controller when asking such questions! That's basic knowlege of boolean algebra, which is some kind of helpful for such kind of projects. And I would not expect such questions from someone who alrady has a logic controller running.

    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.
  • BaggersBaggers Posts: 3,019
    edited 2009-04-10 14:05
    don't panic Reset_Vector, it won't start a fight [noparse]:)[/noparse] lol

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
Sign In or Register to comment.