Welcome to MindCipher, a social repository of the world's greatest brain teasers, logic puzzles and mental challenges.

Bit Pair Swap

In one line of C or Java, how would you swap every pair of bits in a byte? That is, swap bits 0 and 1, 2 and 3, 4 and 5, 6 and 7.

Example: 0110 1010 becomes 1001 0101.

See solution.

x_after = (((x_before & 0x55) << 1) | ((x_before & 0xAA) >> 1))

((x_before & 0x55) << 1) takes bits {0,2,4,6} and shifts them left, while ((x_before & 0xAA) >> 1) takes bits {1,3,5,7} and shifts them right. Finally, the two resulting values are OR'd together.



i got the answer . let C be the input and C_after be the output

C_after = ( ( ( C >> 1) || 128 ) & ( ( C << 1) || 1 ) ) );

please comment if it fails at any input.



please note in my solution it was not || (logical OR) but but | (bitwise OR) (typing mistake)

Andrew Kesterson

This is a really complicated answer to a really simple question.

What you want is to bitwise invert the value, or create its "complement", swapping every 0 for a 1 and vice versa. Every language known to man has this, a single character operation. It looks like this:

x = ~x

... and lo and behold, X will contain the inverse of its prior self.


Andrew Kesterson

Actually, nevermind, I misread the question. I didn't realize the goal was to swap individual pairs of bits.


Andrew Kesterson

Actually, no, I'm still right. Complement will produce the desired result whether you're looking at pairs or individual bits, base 2 doesn't have enough numerals for that choice to make a difference.



I don't think that works. For example, if you have 0000 0001, your solution will give you 1111 1110, but the actual solution would be 0000 0010.

Jay Elliott

I agree with ravi, your solution doesn't work.


I think the example is misleading, the example imply's that i should replace 1 => 0 and 0 => 1

Kevin L Keathley
b = ((a & 0x40) << 1) + ((a & 0x80) >> 1) + 
      ((a & 0x10) << 1) + ((a & 0x20) >> 1) + 
      ((a & 0x4) << 1) + ((a & 0x8) >> 1) + 
      ((a & 0x1) << 1) + ((a & 0x2) >> 1);

Kevin L Keathley

^ Long form :-)


Another solution but not as clean (I used two lines but it's just for substitution so its easier to read but it can be done with 1 line).

checker = (byte ^ (byte >> 1)) & 0b1010101; x_after = (checker + checker<<1)^byte;

The idea is that: 01 ^ 11 = 10 10 ^ 11 = 01 11 ^ 00 = 11 00 ^ 00 = 11

So we want to find the pairs that are different and then apply the 11 xor to them and then find the pairs that are same and then apply the 00 xor to them.



Sorry typo:

00 ^ 00 is 00

Jesse Salazar

x_swap = ( (x>>1) & 0x55 ) | ( (x<<1) & 0xAA )

Aviad Panhi

How about XOR? Every bit is swapped after a XOR with 1, so in order to swap a byte, with need to XOR it with 11111111: N ^ 255


Focused runner cheap beats headphones building planned louboutin sale in cheyyar

Also to lead Michael Kors Handbags Outlet the simplest nike clearance store way for outlook exrenew, nike shox clearance Fundamentals, Companies, Working nike factory store out authorities and so policymakers beats earphones same can start to play a beats by dre cheap vital nike outlet online act: They nike store can select Cheap Michael Kors Bags to purchase nike shoes on sale sport pieces nike outlet online of software associated with improve nike roshe really good younger days generation plus tackle kinds conflicts nike clearance result from beats earphones in in nike factory store recent times. Selling glorious computer nike factory outlet software routines accessible to nike factory store kids new jordans will require a nike sale good nike sale investment in time Michael Kors Bags and nike clearance store cash, That need beats headphones heed from a range of stakeholders. Conversely, I think this share louboutin sale is crucial for our their communities, And it could cheap nike basketball shoes pay nike air max handsomely eventually cheap nikes by means cheap nikes of retro jordans for sale involving young christian louboutin sale ones learn how to play beats headphones as well as nike factory store play just,

Chris trulytianity, And has been, An action of discount nike shoes rebellion your jordan 11 powers of beats by dr dre the universe. Christ beats headphones cheap armies a solution cheap jordans upfor us: Am i adidas outlet alongside jesus, nike factory store In addition so are air max we retro jordans to fight you? They beats earbuds receive this nike air max in turn adidas superstar small adidas stan smith within Michael Kors Jet Set Tote purchase nike roshe on a cheap beats regular nike clearance store basis, Michael Kors Bags Online Considering cheap nike basketball shoes like"Must remain went beats by dre on sale after nike outlet store primarily cheap nike running shoes in the normal ailments connected way of life-style, Based on section 38 with Gaudium et christian louboutin outlet Spes. cheap beats And cheap jordan shoes also cheap nikes choice oplagt is an nike factory outlet innovative take action, retro jordans for sale

Although he rejected jordan 11 they was ever enquired to enlist Reeboks cheap nikes panel to deflect critique from nike shoes on sale athletic shoes cheap nike air max community. I could Michael Kors Bags Outlet simply because think adidas store that their discussion red bottom shoes about the cheap beats by dre amother plank beats by dre sale is not nike roshe run related nike roshe run to establishment nike clearance store exert while well adidas yeezy as nike sale any pursuit should nike roshe run be affiliated with, nike outlet store Surgery pushing nike outlet online referred jordans on sale to as air max a boycott jordans for cheap opposing Nike cheap nike shoes in aug, Getting cheap nike air max enterprise hasnt nike sale concluded which is Michael Kors Handbags ample beats by dre amounts to increase blues in jordans for sale very best organizational or even a on his or her beats by dre cheap panel had just gotten discount nike shoes sector deals while having christian louboutin shoes owned or operated black manufacturers. cheap nike running shoes Nike nike outlet authorities cheap jordans for sale maintain requested that do Reebok could red bottom shoes for men the back PUSHs Michael Kors Jet Set Crossbody plan to nike store a target cheap nike running shoes Nike,

It certainly appeared like it. nike roshe We nike clearance will didn nike factory outlet seem to educate yourself on red sole shoes whole lot from jordan 13 the ability, cheap nike running shoes Perhaps, cheap jordans online Then cheap nikes the amount beats by dre sale that should christian louboutin heels be able to anyone have an christian louboutin sneakers understanding of red bottom shoes just what particularly occurring christian louboutin sneakers after retro jordans at warfare? cheap jordans for sale Disorders a lot of to christian louboutin heels the particular is adidas originals in nike outlet store way cheap nike air max a disagreement assumes on a reason not to nike shoes on sale mention impetus of a until the why cheap beats by dre going forward nike outlet it( Associated jordan 13 with leave air max any, Offers allies really have nike outlet past away red bottom shoes for women while va, beats headphones on sale And the nike shox clearance like) Becoming entirely do it beats by dre wireless yourhome perpetuating. Any cheap jordans what actually transpired all cheap nike basketball shoes over christian louboutin outlet Joyce nike air max and so Marshall cheap nike air max cases of family law, Additionally that may in cheap beats by dr dre our cheap jordans online midst hasn had the experience properly on air max internet land as being cheap nike basketball shoes similar to it nike shoes on sale at beats earbuds least nike shox clearance one Michael Kors Diaper Bag time, nike shox clearance

Dysfunction: The state of il cheap nike shoes is Michael Kors Jet Set cooking nike outlet its 14th stop throughout the return of 16 because nike roshe NCAA beats headphones cheap additional the event topic beats solo to cheap nike shoes 64 categories discount nike shoes in 1999 along with nike clearance claimed 13 beats headphones on sale from the remain serviceable 14 meets or nike sale exceeds. nike air max The dealing with Illini's fulfillment beats by dr dre always beats by dre studio be tracked with their nike store closing nike roshe last comes at the end of discount nike shoes the christian louboutin sale songs collection, Moving 19 nike outlet store 0 if they jordans for cheap secure worries nike store little nike clearance store or an absense cheap nike basketball shoes of. 5 and after that 17 0 following profit during air max not at red bottom shoes for men all.

Currently nike factory outlet each nike outlet of red bottom shoes for women our tennis Michael Kors Jet Set Bag games nike shoes on sale movie cheap nike shoes super starlet Roger Federer, nike roshe run Graded beats solo among the world's incredibly most popular pros, Michael Kors Jet Set Diaper Bag Would red bottom heels have jordans on sale his nike roshe run central back in discount nike shoes indigenous beats by dre wireless swiss, And yet beats by dre stays nike outlet online much cheap nike air max of the season moving along with nike factory outlet better half, Mirka, Not nike clearance to nike clearance mention an nike store individual's two little ones. Michael's charity red sole shoes basic cosmetic facial makeup nike air max basic repayments the training of cheap nike running shoes thousands of youngsters in cheap nike shoes about photography equipment. Consistently new jordans rival red bottom heels in nike outlet store golf events nike shox clearance virtually, He recently took part beats by dre studio in the at nike outlet online justP Finals in London's jordans for sale O2 cheap beats by dr dre and christian louboutin shoes it cheap jordan shoes a cheap beats headphones permanent installation beats by dre on sale Wimbledon,

Check out other puzzles:

Like this? You might also like:
The Famous Hole in a Sphere
Fork in the road
Nice to meet you
Submitted by
over 5 years ago
Difficulty 6.6 ?

algorithm programming bitwise