`{"id":"30473c05-02ee-4bab-a805-59cc9650b6c0","name":"Swap All Odd And Even Bits","description":"1. You are given a number n.\r\n2. You have to swap all odd position bits with even position bits.\r\n3. Every odd position bit is swapped with adjacent bit on left side.\r\n4. Every even position bit is swapped with adjacent bit on right side.\r\n5. Print the number formed after swapping.","inputFormat":"A number n","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n int n;cin>>n;\n \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n //write your code here\r\n \r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10","sampleOutput":"5","questionVideo":"https://www.youtube.com/embed/GbH8PcqKosk?end=57","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"1c943f19-1c76-4d2a-86fe-073a7f68e8ab","name":"Swap All Odd And Even Bits","slug":"swap-all-odd-and-even-bits","type":1}],"next":{"id":"80a2eb81-b13f-4d56-8953-99a92d63e5d4","name":"Swap All Odd And Even Bits MCQ","type":0,"slug":"swap-all-odd-and-even-bits-mcq"},"prev":{"id":"68c8ad58-40a7-4a05-ac19-640cab8115c6","name":"Solve 7n/8","type":3,"slug":"solve-7n-8"}}}`

# Swap All Odd And Even Bits

1. You are given a number n. 2. You have to swap all odd position bits with even position bits. 3. Every odd position bit is swapped with adjacent bit on left side. 4. Every even position bit is swapped with adjacent bit on right side. 5. Print the number formed after swapping.

`{"id":"30473c05-02ee-4bab-a805-59cc9650b6c0","name":"Swap All Odd And Even Bits","description":"1. You are given a number n.\r\n2. You have to swap all odd position bits with even position bits.\r\n3. Every odd position bit is swapped with adjacent bit on left side.\r\n4. Every even position bit is swapped with adjacent bit on right side.\r\n5. Print the number formed after swapping.","inputFormat":"A number n","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n int n;cin>>n;\n \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n //write your code here\r\n \r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10","sampleOutput":"5","questionVideo":"https://www.youtube.com/embed/GbH8PcqKosk?end=57","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"1c943f19-1c76-4d2a-86fe-073a7f68e8ab","name":"Swap All Odd And Even Bits","slug":"swap-all-odd-and-even-bits","type":1}],"next":{"id":"80a2eb81-b13f-4d56-8953-99a92d63e5d4","name":"Swap All Odd And Even Bits MCQ","type":0,"slug":"swap-all-odd-and-even-bits-mcq"},"prev":{"id":"68c8ad58-40a7-4a05-ac19-640cab8115c6","name":"Solve 7n/8","type":3,"slug":"solve-7n-8"}}}`

Editor

# Swap All Odd And Even Bits

easy

1. You are given a number n. 2. You have to swap all odd position bits with even position bits. 3. Every odd position bit is swapped with adjacent bit on left side. 4. Every even position bit is swapped with adjacent bit on right side. 5. Print the number formed after swapping.

1 <= n <= 10^9

## Format

A number n

### Output

Check the sample ouput and question video.

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}10`

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}5`

Question Video

Discussions

Show Discussion

Related Resources