Question

In: Computer Science

Write a program that asks the user to enter an unsigned number and read it. Then...

Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.

COMMENT COMPLETE CODE PLEASE

Solutions

Expert Solution

As per the given problem statement what we understand is we have to first convert the given number in binary format and then separate all the even bits and odd bits, we then left shift the even bits by one position and right shift odd bits by one position, and then merge even and odd bits to get the result.

e.g. 9 = (1001) then even bits are(1000)even and odd bits are (0001)odd and if we left shift even bits by one position then we get (0100)even and right shift odd bits by one position then we get (0010)odd  now we merge the both parts by binary EXOR operation then we get (0110)result i.e. 6.

To get the even bits in the given number we can AND it with 0xAAAAAAAA. And then we can right shift with >> operator.

To get the odd bits in the given number we can AND it with 0x55555555. And then we can left shift with << operator.

As you haven't mentioned any particular language in the question I will code it using JAVA as follows

import java.util.Scanner;

public class SwapEvenOddBits {
    
        static int swapBits(int num) 
    { 
        //get even bits of number and right shift it by 1
        int evenBits = num & 0xAAAAAAAA;  
        evenBits >>= 1;
      
        // Get even bits of number and left shift it by 1 
        int oddBits = num & 0x55555555;
        oddBits <<= 1;  
          
        // merge even and odd bits 
        int result = (evenBits | oddBits);
        
        return result;  
    }
    
    public static void main(String[] args) 
    { 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the integer number");
        int num = sc.nextInt();
        System.out.println("Given number after swapping even odd bits is "+swapBits(num)); 
        sc.close();
        
    }
}

Output :


Related Solutions

Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0. using for-loop
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT