0

I would like to find a better way to achieve the following:

n = 6
k = 1

Flip the 1st significant bit in 6

Variable      Binary representation      Decimal Representation
       n                        110                           6
m(result)                       010                           2

I would like to achive the same as in this wiki article

Here is what I did, but I find it a bit overkill, an unefficient:

def toggleKthSignificantBit(self, n, k):
        tmp = list(bin(n)[2:].zfill(3))
        tmp[k-1] = str(int(tmp[k-1]) ^ 1)
        tmp2 = ''.join(tmp)
        print(tmp2)
        return int(tmp2, 2)
Bence Szabari
  • 143
  • 1
  • 2
  • 12

3 Answers3

0

IIUC perhaps

n ^ 2**(len(bin(n)) - (2 + k))

def toggle_kth_left_to_right(n, k):
    res = n ^ 2**(len(bin(n)) - (2+k))
    return bin(n)[2:], bin(res)[2:]

# toggle_kth_left_to_right(6, 1)
# ('110', '10')

# toggle_kth_left_to_right(6, 2)
# ('110', '100')

# toggle_kth_left_to_right(6, 3)
# ('110', '111')
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

Assuming k is the position of the bit

Then this should do the trick

x=n^(2**k)
bin(x)

ADDENDUM: if we're counting bits from the left then we can do the following

x = n^(2**(int(n).bit_length()-k))
bin(x)
Ilia Gilmijarow
  • 1,000
  • 9
  • 11
  • That's the correct answer if k is counting zero based from right to left (which is what we are used to mathematically). However, I have the impression OP is counting one based from left to right. – SpghttCd Oct 23 '19 at 08:57
  • Yes, in this case the indexing is left to right, since the most significant bit (MSB, also called the high-order bit) is the bit position in a binary number having the greatest value. – Bence Szabari Oct 23 '19 at 08:59
  • I think, the first line is the solution for my problem, but I found an equivalent one. – Bence Szabari Oct 23 '19 at 09:03
0

After doing some examination, I found a solution for my question:

However, I think I find a equivalent solution:

n ^ (2**(butterfly_rank - k))
Bence Szabari
  • 143
  • 1
  • 2
  • 12
  • Then you should edit your question, because as stated in the comments and confirmed by you the accepted answer does _not_ what you asked. – SpghttCd Oct 23 '19 at 09:07
  • I run both this solution and llia's solution and I got the same results – Bence Szabari Oct 23 '19 at 09:10
  • Yes but you _asked_ for the opposite; see my comment under Ilia's answer and your own confirmation. – SpghttCd Oct 23 '19 at 09:12
  • So, what should I do then? As far as I understand lila's answer works if we start from 0 right to left, and mine is when we start from 1 left to right. But I still see the same result, then what should I accept? – Bence Szabari Oct 23 '19 at 09:17
  • No - IMO your answer and Ilia's answer do completely the same: counting zero based from right to left. In opposition to that: your question and my answer count one based from left to right. – SpghttCd Oct 23 '19 at 09:19
  • Don't get me wrong - as I also already stated counting zero based from right to left is nicer, cleaner and we're used to it mathematically. But if you also feel so and want to accept that, then you should at least change your question, otherwise you will accept an answer which does not fit to your question - and this is not good for a Q&A site. – SpghttCd Oct 23 '19 at 09:22
  • Okay, I'll do that. But first check this [link](https://en.wikipedia.org/wiki/Butterfly_network#Butterfly_network_building) and what do you say, what should be the correct way to get the flipped m value?. It's still possible that I asked the wrong quiesion. – Bence Szabari Oct 23 '19 at 09:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201308/discussion-between-bence-szabari-and-spghttcd). – Bence Szabari Oct 23 '19 at 09:31
  • Sorry - don't have the time. If I look over your link I _think_ they count one based from left to right, so your question and my answer fit. However, it's a matter of taste, because even if you want to implement something about this topic on this wiki site,the implementation details are up to you, and if you find `2 ^ (1< – SpghttCd Oct 23 '19 at 09:34
  • look at Ilia's answer - they're addressing every singe possibility in the meantime, so there you'll find definitely something... :) – SpghttCd Oct 23 '19 at 09:38