431,805 Members | 1,273 Online
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Sep 19 '06 #1
6 Replies

 P: n/a In article , NM I am given an int and have to tell whether the bit representation of thatint 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 toknow if the signed to unsignedconversion is changing the bit pattern or not? It works on my system, butdon't know if it will work onevery 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 Sep 20 '06 #2

 P: n/a 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" , NM >I am given an int and have to tell whether the bit representation of thatint 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 wanttoknow if the signed to unsignedconversion is changing the bit pattern or not? It works on my system, butdon't know if it will work onevery 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 Sep 20 '06 #3

 P: n/a NM The question is what is C++ says about int to unsigned int conversion. Theanswer I found so far is that when a signed type with negative value isstored in an unsigned type the value stored is the negative value modulonumber 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 Sep 20 '06 #4

 P: n/a 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 template 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)<::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 Sep 20 '06 #5

 P: n/a Frederick Gotham posted: operator bool() const { return val & T(1)<

 P: n/a 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: Sep 20 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.