i'm new to bitwise operators and I'm trying to show the palindrome representation of a number , for n= 32 ( 0010 0000 ) it should output 4 (0000 0100) but the values that it gives me are random, for example for the above input , instead of 4 , i get an 2, and if i try 3 ( 00000011) it doesn't give me 192 11000000 ...it gives me 6 (0000 0110) and so on ... i'm sure the code is wrong , i just want to know where. BTW i've checked other post but i coudn't figure it out
#include <iostream>
using namespace std;
int main ()
{
int n,k;
cout<<"Dati n: ";cin>>n;
while(n>0){
k=k<<1;
if(n & 1 == 1)
k |= 1<<1;
n=n>>1U;
}
cout<<k;
}