• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
October 25, 2023 |630 Views
SDE Sheet - Swap all odd and even bits
  Share  2 Likes
Description
Discussion

This video is part of the Bit Manipulation section under the GFG SDE Sheet.

In this problem, we are given, an unsigned integer N. The task is to swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43(00101011). Here, every even position bit is swapped with an adjacent bit on the right side(even position bits are highlighted in the binary representation of 23), and every odd position bit is swapped with an adjacent on the left side.

Example :

Input: N = 23
Output: 43

Explanation: 
Binary representation of the given number 
is 00010111 after swapping 
00101011 = 43 in decimal.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/swap-all-odd-and-even-bits/
Problem: https://practice.geeksforgeeks.org/problems/swap-all-odd-and-even-bits-1587115621/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More