Page 2 of 2

Re: Reverse a single bit in an integer

Posted: Thu Mar 18, 2010 8:33 pm
by Kaeru Gaman
I already had that idea, but it does not work:

Code: Select all

n = 0
t = n ! %101
Debug Str(n) + ": " + RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")

n = 2
t = n ! %101
Debug Str(n) + ": " + RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")

n = 5
t = n ! %101
Debug Str(n) + ": " + RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")

n = 7
t = n ! %101
Debug Str(n) + ": " + RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0")
it does not flip bits 0 and 2, it toggles them each for each.

Re: Reverse a single bit in an integer

Posted: Thu Mar 18, 2010 8:41 pm
by netmaestro
You can, as Kaeru pointed out at the beginning. I coded mine as I did mainly for a version that is easy to understand when you read the code.

Re: Reverse a single bit in an integer

Posted: Fri Mar 19, 2010 3:08 am
by rsts
I realize my problem statement left WAY too much open to interpretation, especially when I spoke of flipping bits.

What I was attempting to do was swap (not flip), the rightmost bit with the leftmost bit in the three bit combination, leaving the middle bit unchanged.

Somehow, Mr Vogel was able to correctly interpret my question and provide a solution - which i still don't fully understand (i don't understand how he correctly interpreted my question, nor do I fully understand the solution yet, but I'm getting there).

Thanks to all of you. Next time I'll spend more time on the problem description.

cheers

Re: Reverse a sin gle bit in an integer

Posted: Fri Mar 19, 2010 7:20 am
by Michael Vogel
rsts wrote:You give this to a guy who's still trying to figure out XOR? :evil:
So you get some more XORs as an extra :wink:

Code: Select all

For n=0 To 7

   ; bits of n : ...6543210

   ; (get rid of bit 0 and bit 2) plus (bit 0 on position 2) plus (bit 2 shifted to position 0)
   t = (n&%1111010) | ((n&1)<<2) | ((n>>2)&1)
   
  ; slow mode (from the ministry of silly walk)...
   b0=(n)&1        ; get value of bit 0
   b2=(n>>2)&1   ; get value of bit 2
   u= n ! b0 ! b2 ! (b2<<2) ! (b0<<2)    ; remove old value of bits 0 and 2 and put new values into the integer value
   
   Debug RSet(Bin(n),3,"0") + " -> " + RSet(Bin(t),3,"0") + " -> " + RSet(Bin(u),3,"0")
Next
If you have enough, just add a "[Done]" to your posting title :lol:
Michael

Re: Reverse a single bit in an integer

Posted: Fri Mar 19, 2010 9:37 am
by Kaeru Gaman
I was thinking 'bout some similar solution like Michael showed...

his way:

Code: Select all

t = (n&%1111010) | ((n&1)<<2) | ((n>>2)&1)
maybe it is a bit confusing...

1. you did not mention the higher bits, so we leave them out.
Michael preserved them for wich reason ever.

Code: Select all

t = ( n & %010 ) | ( (n & 1) << 2 ) | ( ( n >> 2 ) & 1)
2. Michael mixed up the order to shift and mask.
one bit he shift first, the other he mask first.
maybe it's getting clearer when we apply the same method on both.
additionally, we write the mask in binary.

Code: Select all

t = ( n & %010 ) | ( (n & %001) << 2 ) | ( ( n & %100 ) >> 2 )
3. last, we change the order of the operations to come to maximum readabilyty.

Code: Select all

t = ( ( n & %100 ) >> 2 ) | ( n & %010 ) | ( (n & %001) << 2 )
it is just the same, but clearer to see.

we access three bits
the left one is isolated then shifted to the right by 2
the middle one is isolated to be taken just the way it is
the right one is isolated and shifted to the left by 2

it's still cryptic enough, but maybe a bit less now. ;)

Re: [DONE] Swap 2 bits in an integer

Posted: Fri Mar 19, 2010 10:26 am
by rsts
And again, thanks all for the solutions.

Mr Gaman, are you a teacher? You have a nice way of explaining things. Your post http://www.purebasic.fr/english/viewtop ... it=bitwise was also very well explained.

If any of you are ever in Cincinnati, drinks are on me.

Re: [DONE] Swap 2 bits in an integer

Posted: Fri Mar 19, 2010 11:03 am
by Kaeru Gaman
thank you. :)

no, I'm not an educated teacher, but I like to share knowledge and trained the ways how to explain things for decades.
if I ever find the patience I'll start writing books... ;)