Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What happens when type conversion between signed and unsigned happens?

Reply
Thread Tools

What happens when type conversion between signed and unsigned happens?

 
 
NM
Guest
Posts: n/a
 
      09-20-2006
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


 
Reply With Quote
 
 
 
 
Steve Pope
Guest
Posts: n/a
 
      09-20-2006
In article <eeq10d$52o$(E-Mail Removed)>, NM <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
NM
Guest
Posts: n/a
 
      09-20-2006
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" <(E-Mail Removed)> wrote in message
news:eeq5s6$2t4$(E-Mail Removed)...
> In article <eeq10d$52o$(E-Mail Removed)>, NM <(E-Mail Removed)> 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



 
Reply With Quote
 
Steve Pope
Guest
Posts: n/a
 
      09-20-2006
NM <(E-Mail Removed)> 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
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-20-2006
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>::digit s) );
}


> 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
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-20-2006
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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-20-2006
NM wrote:

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




Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Conversion rules between unsigned operands and signed operand somenath C Programming 11 07-30-2007 10:37 AM
Conversion from Unsigned to Signed for bitwise. codefixer@gmail.com C Programming 3 02-04-2005 07:03 AM
Explicit unsigned/signed conversion: ANSI/ISO rules? Ken Tough C Programming 4 06-08-2004 04:38 PM
silly unsigned/signed byte conversion question aspa Java 9 10-29-2003 10:37 PM



Advertisments