-1

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;

}
  • 2
    Sure, the printed values will be _random_. `k=k<<1;` is undefined behavior, since, on the first iteration of the loop, `k` is uninitialized, and reading such value invokes undefined behavior. Next time, step through your code with a debugger first. – Algirdas Preidžius Mar 07 '18 at 09:19
  • 1
    Remember that uninitialized local variables (like your variable `k`) will haven an *indeterminate* (and seemingly random) value. Using uninitialized variables leads to *undefined behavior* in C++. – Some programmer dude Mar 07 '18 at 09:19
  • My code isn't doing what it's supposed to, its outputing random values – Anghel Alexandru Mar 07 '18 at 09:19
  • I've initialized k as 0 and 1 and it still doesn't work , i'm not sure what i'm doing wrong – Anghel Alexandru Mar 07 '18 at 09:23
  • 1
    `k |= 1<<1;` is wrong, too. That should be `k |= 1;`. You are performing the left shifting in `k=k<<1;` already. `while(n>0)` is a bug as well. You fail to account for the upper zero bits, and will not shift `k` left for those. You should consider taking a step back, and get the basics straight. – IInspectable Mar 07 '18 at 09:24
  • 1
    @AnghelAlexandru 1) Your program does exactly what you tell it to. If you are not sure about an algorithm, solve it with a pen and paper, before converting it to code. If your code doesn't do correct thing - step through the code with a debugger, to figure out why. 2) Your example is, still, not updated with such information. Hence, from our perspective - the issue is still there. – Algirdas Preidžius Mar 07 '18 at 09:25
  • Do you need to invert `n` in bit representation or do you want to check if `n` is a palindrome(sequence of characters which reads the same backward as forward)? – Smit Ycyken Mar 07 '18 at 09:36

2 Answers2

0

A possible solution is to use std::bitset:

std::bitset<8> bs(3); // 0000 0011
std::string palindrome = bs.to_string(); // "0000 0011"
// reverse the string representation
std::reverse(palindrome.begin(), palindrome.end()); // "1100 0000"

std::cout << bs.to_ulong() << std::endl;
std::cout << std::bitset<8>(palindrome).to_ulong();

Give this result:

3
192
thibsc
  • 3,747
  • 2
  • 18
  • 38
-1

If you are using GCC, you can use one of its builtin functions:

uint32_t palindrome( uint32_t num ) {
   return __builtin_bswap32(num);
}

palindrome(2U) will return 33554432 (0x2000000)

The advantage is that the compiler will use the binary swap instruction in the processor, if available. For x86_64, for example:

palindrome(unsigned int):
  mov eax, edi
  bswap eax
  ret
Jorge Bellon
  • 2,901
  • 15
  • 25
  • i'm using dev c++ i don't know if it has GCC compiler, because it doesn't register my function as "uint32_t" type – Anghel Alexandru Mar 07 '18 at 09:43
  • That swaps **bytes**. Swapping bytes does not produce a bitwise 'palindrome'. Notwithstanding the fact, that the OP isn't even looking for palindromes, but bitwise reversal. – IInspectable Mar 07 '18 at 09:58