[DONE] Swap 2 bits in an integer

Just starting out? Need help? Post your questions and find answers here.
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 »

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.
oh... and have a nice day.
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 »

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.
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 »

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
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Reverse a sin gle bit in an integer

Post 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
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 »

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. ;)
oh... and have a nice day.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: [DONE] Swap 2 bits in an integer

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: [DONE] Swap 2 bits in an integer

Post 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... ;)
oh... and have a nice day.
Post Reply