Write a program to swap odd and even bits in integer what is the minimum number of steps required?

梦想的初衷 提交于 2021-02-18 17:42:47

问题


I am trying to solve this one..and my code is as follows

#include<stdio.h>
int main() {
    int a, b = 0xaaaaaaaa, c = 0x55555555;
    printf("\n enter the number: \n");
    scanf("%d", & a);
    a = ((a & b) >> 1) | ((a & c) << 1);
    printf("\n %d", a);
}

..but i am getting some weird outputs..can anyone tell me what errors i m making?


回答1:


Your idea is ok. Perhaps you're getting the weird outputs because the bitshift don't work exactly as you may expect.

Your variables are of type int. Means - they are signed. Now, when you do the bitshift to a signed integer, there are additional rules about how the MSB bit is propagated. In simple words, when a signed integer is shifted right, the MSB isn't necessarily zero, it's copied from the old MSB value.

Try to replace int by unsigned int.




回答2:


on my pc this code worked perfectly just changed the plus to |

#include <stdio.h>

int main() {
    int a, b = 0xaaaaaaaa, c = 0x55555555;
    printf("\n enter the number: \n");
    scanf("%d", & a);
    a = ((a & b) >> 1) | ((a & c) << 1);
    printf("\n %d\n", a);
}

OUTPUT:

 enter the number: 
2
 1    
 enter the number: 
1
 2



回答3:


Though this solution takes more iterations, for understandable purposes try this

void swapEvenOddBits()    //function to swap the even and odd bits
{       
    unsigned int num=0,even=0,odd=0;
    scanf("%u",&num);  //enter the number

    for(int i=1; i<32; i=i+2){
        even=num&(1<<(i-1));   
        odd=num&(1<<i);     
        num=num-even-odd;   

        even=even<<1;       
        odd=odd>>1;         
        num=num+even+odd;   
        //printf("%u  %d:%d  %d:%d  \n",num,i-1,even,i,odd); //track iterations with this
    }
    printf("%u",num);  //end result
}



回答4:


unsigned char
swapOddEvenBits(unsigned char num)
{
    unsigned char odd_bits = num & 0xAA;
    unsigned char even_bits = num & 0x55;

    odd_bits >>= 1;
    even_bits <<= 1;

    return (odd_bits | even_bits);
}



回答5:


Reason of your issue, is right shifting of signed integral number, where last two digits will have 11 as you ORed the last nibble with 1010. after OR the last nibble would look like 1101.




回答6:


import java.io.*;
public class EvenOdd {

    public static void main(String[] args) 
    {
        int b = 0xaaaaaaaa, c = 0x55555555;
        System.out.println("enter number:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n="";
        try {
            n = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int num = Integer.parseInt(n);
        num = ((num&b)>>1)|((num&c)<<1);
        System.out.println(num);
    }
}


来源:https://stackoverflow.com/questions/6495854/write-a-program-to-swap-odd-and-even-bits-in-integer-what-is-the-minimum-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!