Page 1 of 2
[DONE] Swap 2 bits in an integer
Posted: Thu Mar 18, 2010 7:06 pm
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
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:08 pm
by Kaeru Gaman
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:17 pm
by rsts
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
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:21 pm
by Arctic Fox
rsts wrote:Modulo? I never even considered that!
Isn't it a binary number

(like $ for hexadecimal numbers)
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:25 pm
by rsts
My mistake
Anyway, doesn't work for %000, which should remain 000
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:34 pm
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()

Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:38 pm
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.
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:41 pm
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.

Like I said - I've been at this too long
cheers
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:44 pm
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...
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:46 pm
by Arctic Fox
OK, I got it now, too - forget my previous post

Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 7:58 pm
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
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 8:05 pm
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?
This forum is the best

Thanks, Mr Vogel.
cheers
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 8:07 pm
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")
Re: Reverse a single bit in an integer
Posted: Thu Mar 18, 2010 8:13 pm
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
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
Posted: Thu Mar 18, 2010 8:27 pm
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")