开发者

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

开发者 https://www.devze.com 2023-03-15 13:39 出处:网络
I am trying to solve this one..and my code is as follows #include<stdio.h> int main() { int a, b = 0xaaaaaaaa, c = 0x55555555;

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?


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.


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


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
}


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);
}


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.


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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消