# What happens when type conversion between signed and unsigned happens?

Discussion in 'C++' started by NM, Sep 20, 2006.

1. ### NMGuest

I am given an int and have to tell whether the bit representation of that
int is a palindrome or not.
Here is what I have come up with

bool isIntPalindrome(int num)
{
unsigned int temp = num;
int reversed = 0;

while (num != 0) {
reveresed <<= 1;
reversed |= (temp & 1);
temp >>= 1;
}
return reversed == num;
}

My question is this doing what it is supposed to do? In particular I want to
know if the signed to unsigned
conversion is changing the bit pattern or not? It works on my system, but
don't know if it will work on
every machine.

Thanks

NM

NM, Sep 20, 2006

2. ### Steve PopeGuest

In article <eeq10d\$52o\$>, NM <> wrote:

>I am given an int and have to tell whether the bit representation of that
>int is a palindrome or not.
>Here is what I have come up with

>bool isIntPalindrome(int num)
>{
> unsigned int temp = num;
> int reversed = 0;
>
> while (num != 0) {
> reveresed <<= 1;
> reversed |= (temp & 1);
> temp >>= 1;
> }
> return reversed == num;
>}

>My question is this doing what it is supposed to do? In particular I want to
>know if the signed to unsigned
>conversion is changing the bit pattern or not? It works on my system, but
>don't know if it will work on
>every machine.

I see what you're doing (although I think the code you posted
may not be exact); I personally would not count on this working, and
would separately test for the MSB and LSB being equal.

Another issue is that you're looking for a palindrome in the
full width of the int, which may be different on different platforms.
On a 64 bit machine, you get different palindromes than on a 32
bit machine. More useful might be a palindrome test that looks only
at the k LSB's of the int, for some parameter k.

Steve

Steve Pope, Sep 20, 2006

3. ### NMGuest

The question is what is C++ says about int to unsigned int conversion. The
answer I found so far is that when a signed type with negative value is
stored in an unsigned type the value stored is the negative value modulo
number of values the unsigned type can hold. Also according to modulo
arithmatic in C++ when only one of the operand is negative the value and
sign of the result is machine dependent.
These two description seems confusing. For example we know that signed ->
unsigned conversion will call a modulo operation and the result must be
positive because the unsigned type cannot hold negative number. But modulo
arithmatic says the sign is machine dependent.

Can someone show some light here. Is it true that the conversion here
"int -> unsigned int" may change the bit pattern?

Thanks

"Steve Pope" <> wrote in message
news:eeq5s6\$2t4\$...
> In article <eeq10d\$52o\$>, NM <> wrote:
>
>>I am given an int and have to tell whether the bit representation of that
>>int is a palindrome or not.
>>Here is what I have come up with

>
>>bool isIntPalindrome(int num)
>>{
>> unsigned int temp = num;
>> int reversed = 0;
>>
>> while (num != 0) {
>> reveresed <<= 1;
>> reversed |= (temp & 1);
>> temp >>= 1;
>> }
>> return reversed == num;
>>}

>
>>My question is this doing what it is supposed to do? In particular I want
>>to
>>know if the signed to unsigned
>>conversion is changing the bit pattern or not? It works on my system, but
>>don't know if it will work on
>>every machine.

>
> I see what you're doing (although I think the code you posted
> may not be exact); I personally would not count on this working, and
> would separately test for the MSB and LSB being equal.
>
> Another issue is that you're looking for a palindrome in the
> full width of the int, which may be different on different platforms.
> On a 64 bit machine, you get different palindromes than on a 32
> bit machine. More useful might be a palindrome test that looks only
> at the k LSB's of the int, for some parameter k.
>
> Steve

NM, Sep 20, 2006
4. ### Steve PopeGuest

NM <> wrote:

>The question is what is C++ says about int to unsigned int conversion. The
>answer I found so far is that when a signed type with negative value is
>stored in an unsigned type the value stored is the negative value modulo
>number of values the unsigned type can hold.

Sounds reasonable, and definitely this was the treatment in C.

> Also according to modulo arithmatic in C++ when only one of
> the operand is negative the value and sign of the result is
> machine dependent.

>These two description seems confusing.

I would guess the second description is meant to apply to
using the modulo operator on two (signed) int's, while
in the first description the term "modulo" has its normal
mathematical meaning. The unsigned type *can't* hold
a negative value, and there is only one value modulo its
range that is equal to a given negative value.

Steve

Steve Pope, Sep 20, 2006
5. ### Frederick GothamGuest

NM posted:

> I am given an int and have to tell whether the bit representation of
> that int is a palindrome or not.

Many people have written a template function which determines whether an
array is a palindrome. If you want to be fancy, you can define your own
class called BitHandle which works exactly like a pointer, and then pass
this object to the pre-written (and maybe even very efficient) palindrome
function. Something like:

Warning: Buggy, sloppily written code...

#include <limits>

template<class T>
class BitHandle {
private:
T const val;
unsigned bit_index;

public:

BitHandle(T const val, unsigned const i)
: val(arg), bit_index(i) {}

BitHandle &operator++() { ++bit_index; return *this; }
BitHandle &operator--() { --bit_index; return *this; }

operator bool() const
{
return val & T(1)<<bit_index;
}
};

int main()
{
unsigned val = 72;

TemplatePalindromeFunction(
BitHandle(val,0),
BitHandle(val,std::numeric_limits<unsigned>::digits) );
}

> bool isIntPalindrome(int num)
> {
> unsigned int temp = num;
> int reversed = 0;
>
> while (num != 0) {
> reveresed <<= 1;

Here you shift zero to the left by one place. That will have no effect
whatsoever.

> reversed |= (temp & 1);

Here you test if "temp" has its low-order bit set, and it does, then you
set reversed's low-order bit.

> temp >>= 1;

You shift temp once to the right, effectively dividing it by 2.

--

Frederick Gotham

Frederick Gotham, Sep 20, 2006
6. ### Frederick GothamGuest

Frederick Gotham posted:

> operator bool() const
> {
> return val & T(1)<<bit_index;
> }

That probably should have been:

bool operator*() const

(i.e. the dereference operator)

--

Frederick Gotham

Frederick Gotham, Sep 20, 2006
7. ### Default UserGuest

Re: What happens when type conversion between signed and unsigned happens? [TPA]

NM wrote:

> The question is what is C++ says about int to unsigned int