Shop OBEX P1 Docs P2 Docs Learn Events
switching a 0 to a 1 and so on... — Parallax Forums

switching a 0 to a 1 and so on...

ReachReach Posts: 107
edited 2014-04-17 17:14 in Propeller 1
In Spin

What is the simplest way to flip a bit 0 ot a 1 in a variable long without changing the other bits. As well as switch a 1 to a 0.

Example

Temp := 0000,0000,0000,0000,0000,0000,0000,0000

Now I want to turn bit 3 from a 0 to a 1

Temp := 0000,0000,0000,0000,0000,0000,0000,0100

Comments

  • ReachReach Posts: 107
    edited 2014-04-17 13:49
    Can someone run this and tell me if element is 511


    CON
      _clkmode   = xtal1 + pll16x                           
      _xinfreq   = 5_000_000
    
    
    VAR
    
    
      long  element   
      long  numb[9]
    
    
    Pub main | temp, v
    
    
                '%0000_0000_0000_0000_0000_0001_1111_1111
      numb[0] := %0000_0000_0000_0000_0000_0000_0000_0001 
      numb[1] := %0000_0000_0000_0000_0000_0001_0000_0000 
      numb[2] := %0000_0000_0000_0000_0000_0000_0000_0100
      numb[3] := %0000_0000_0000_0000_0000_0000_1000_0000
      numb[4] := %0000_0000_0000_0000_0000_0000_0000_1000
      numb[5] := %0000_0000_0000_0000_0000_0000_0000_0010
      numb[6] := %0000_0000_0000_0000_0000_0000_0001_0000 
      numb[7] := %0000_0000_0000_0000_0000_0000_0100_0000 
      numb[8] := %0000_0000_0000_0000_0000_0000_0010_0000 
      
      V:= 0
      element := %0000_0000_0000_0000_0000_0000_0000_0000
      
      Repeat 9  
        Case numb[v]
          1:  temp := %0000_0000_0000_0000_0000_0000_0000_0001
          2:  temp := %0000_0000_0000_0000_0000_0000_0000_0010
          3:  temp := %0000_0000_0000_0000_0000_0000_0000_0100
          4:  temp := %0000_0000_0000_0000_0000_0000_0000_1000
          5:  temp := %0000_0000_0000_0000_0000_0000_0001_0000
          6:  temp := %0000_0000_0000_0000_0000_0000_0010_0000
          7:  temp := %0000_0000_0000_0000_0000_0000_0100_0000
          8:  temp := %0000_0000_0000_0000_0000_0000_1000_0000
          9:  temp := %0000_0000_0000_0000_0000_0001_0000_0000
          0:  temp := %0000_0000_0000_0000_0000_0000_0000_0000
          
        v :=   v + 1
        element := element ^ temp
    
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-17 16:17
    There's an easier way:
    pub flip_bit(value, bit)
    
      if ((bit => 0) and (bit =< 31))
        value ^= 1 << bit
    
      return value
    
  • ReachReach Posts: 107
    edited 2014-04-17 17:14
    Thanks JonnyMac and Ron I needed that. :thumb:

    I dont have my propeller kits - power on the sail boat is low and so I am trying to do a little something with that code up there. I needed someone to run that code for me but I can wait for sun to charge my battery's. In fact my laptop is almost dead. :)
Sign In or Register to comment.