[DONE] Swap 2 bits in an integer
[DONE] Swap 2 bits in an integer
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
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.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: Reverse a single bit in an integer
what about XOR?
Code: Select all
value ! %101
oh... and have a nice day.
Re: Reverse a single bit in an integer
I almost addressed the topic to you since I knew you were one of the experts in bitwise
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

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
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
Re: Reverse a single bit in an integer
Isn't it a binary numberrsts wrote:Modulo? I never even considered that!

Re: Reverse a single bit in an integer
My mistake
Anyway, doesn't work for %000, which should remain 000

Anyway, doesn't work for %000, which should remain 000
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
Re: Reverse a single bit in an integer
How about "the string way"?
Edit Updated with the use of RSet() 
Code: Select all
Val(ReverseString(RSet(Bin(value), 3, "0")))

- netmaestro
- 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
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.Anyway, doesn't work for %000, which should remain 000
BERESHEIT
Re: Reverse a single bit in an integer
Of course it would.netmaestro wrote: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.Anyway, doesn't work for %000, which should remain 000


cheers
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: Reverse a single bit in an integer
indeed, that's what I meant...
but this does nit really flip bit 0 and 2, it just reverses the two each.
the ReverseString solution should work better...
Code: Select all
For n=0 To 7
t = n ! %101
Debug RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")
Next
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.
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
Re: Reverse a single bit in an integer
OK, I got it now, too - forget my previous post



- Michael Vogel
- Addict
- Posts: 2797
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Reverse a single bit in an integer
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
Re: Reverse a single bit in an integer
You give this to a guy who's still trying to figure out XOR?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

This forum is the best

cheers
- netmaestro
- 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
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
Re: Reverse a single bit in an integer
AHAnetmaestro 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")

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

Re: Reverse a single bit in an integer
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