What happens when type conversion between signed and unsigned happens?

N

NM

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
 
S

Steve Pope

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
 
N

NM

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
 
S

Steve Pope

NM said:
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
 
F

Frederick Gotham

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.
 
F

Frederick Gotham

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top