[DONE] Swap 2 bits in an integer

Just starting out? Need help? Post your questions and find answers here.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

[DONE] Swap 2 bits in an integer

Post by rsts »

I'm actually attempting to "flip" the two end bits in a 3 bit combo while keeing the middle bit constant: The higher order bits may stay the same.

011 becomes 110 or 001 becomes 100. I can NOT it and reverse it all, but then how do I NOT the middle bit by itself?

Or what's the better way?

cheers
Last edited by rsts on Fri Mar 19, 2010 10:20 am, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Reverse a single bit in an integer

Post by Kaeru Gaman »

what about XOR?

Code: Select all

value ! %101
oh... and have a nice day.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Reverse a single bit in an integer

Post by rsts »

I almost addressed the topic to you since I knew you were one of the experts in bitwise :D

Modulo? I never even considered that!

Take me a few mins to wrap my brain around this. Might be back with a question if I can't :)

Once again, thanks, Mr Gaman.

cheers
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: Reverse a single bit in an integer

Post by Arctic Fox »

rsts wrote:Modulo? I never even considered that!
Isn't it a binary number :?: (like $ for hexadecimal numbers)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Reverse a single bit in an integer

Post by rsts »

My mistake :oops:

Anyway, doesn't work for %000, which should remain 000
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: Reverse a single bit in an integer

Post by Arctic Fox »

How about "the string way"?

Code: Select all

Val(ReverseString(RSet(Bin(value), 3, "0")))
Edit Updated with the use of RSet() :mrgreen:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Reverse a single bit in an integer

Post by netmaestro »

Anyway, doesn't work for %000, which should remain 000
I don't get that from your post. You said you wanted to flip bits 0 and 2 from whatever they were, didn't you? Then 000 would become 101.
BERESHEIT
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Reverse a single bit in an integer

Post by rsts »

netmaestro wrote:
Anyway, doesn't work for %000, which should remain 000
I don't get that from your post. You said you wanted to flip bits 0 and 2 from whatever they were, didn't you? Then 000 would become 101.
Of course it would. :oops: Like I said - I've been at this too long :)

cheers
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Reverse a single bit in an integer

Post by Kaeru Gaman »

indeed, that's what I meant...

Code: Select all

For n=0 To 7
  t = n ! %101
  Debug RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")
Next
but this does nit really flip bit 0 and 2, it just reverses the two each.

the ReverseString solution should work better...
Last edited by Kaeru Gaman on Thu Mar 18, 2010 7:46 pm, edited 2 times in total.
oh... and have a nice day.
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: Reverse a single bit in an integer

Post by Arctic Fox »

OK, I got it now, too - forget my previous post :wink: :)
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Reverse a single bit in an integer

Post by Michael Vogel »

Code: Select all

For n=0 To 7
	t = (n&%1111010) | ((n&1)<<2) | ((n>>2)&1)
	Debug RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")
Next
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Reverse a single bit in an integer

Post by rsts »

Michael Vogel wrote:

Code: Select all

For n=0 To 7
	t = (n&%1111010) | ((n&1)<<2) | ((n>>2)&1)
	Debug RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")
Next
You give this to a guy who's still trying to figure out XOR? :evil:

This forum is the best :D Thanks, Mr Vogel.

cheers
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Reverse a single bit in an integer

Post by netmaestro »

For readability, here's my two cents:

Code: Select all

#bit2 = 4
#bit0 = 1

Macro FlipBits_0_and_2(number)
  If number & #bit2
    number &~ #bit2
  Else
    number | #bit2
  EndIf
  
  If number & #bit0
    number &~ #bit0
  Else
    number | #bit0
  EndIf
EndMacro

number = %000

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")
BERESHEIT
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Reverse a single bit in an integer

Post by rsts »

netmaestro wrote:For readability, here's my two cents:

Code: Select all

#bit2 = 4
#bit0 = 1

Macro FlipBits_0_and_2(number)
  If number & #bit2
    number &~ #bit2
  Else
    number | #bit2
  EndIf
  
  If number & #bit0
    number &~ #bit0
  Else
    number | #bit0
  EndIf
EndMacro

number = %000

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")
AHA :shock:

Now it begins to sink in.

You would never believe I was a math major. But that was back in the days of sliderules.

The best to all of you :D
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Re: Reverse a single bit in an integer

Post by Deeem2031 »

I don't see why you don't use the binary XOR operator:

Code: Select all

#bit2 = 4
#bit0 = 1

Macro FlipBits_0_and_2(number)
  number ! #bit2
  
  number ! #bit0
EndMacro

number = %000

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")

FlipBits_0_and_2(number)

Debug RSet(Bin(number),3,"0")
irc://irc.freenode.org/#purebasic
Post Reply