473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What happens when type conversion between signed and unsigned happens?

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
Sep 19 '06 #1
6 2329
In article <ee**********@news.cs.utexas.edu>, NM <nm@nm.comwrote:
>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
Sep 20 '06 #2
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

"Steve Pope" <sp*****@speedymail.orgwrote in message
news:ee**********@blue.rahul.net...
In article <ee**********@news.cs.utexas.edu>, NM <nm@nm.comwrote:
>>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

Sep 20 '06 #3
NM <nm@nm.comwrote:
>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
Sep 20 '06 #4
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
Sep 20 '06 #5
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
Sep 20 '06 #6
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>
Sep 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
49
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer....
1
by: b83503104 | last post by:
When are they not consistent?
5
by: pankaj tiwary | last post by:
Hello experts, please consider the code fragment:- #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array)/sizeof(array)) int array = {23,34,12,17,204,99,16}; int main() {
5
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
48
by: Frederick Gotham | last post by:
The "toupper" function takes an int as an argument. That's not too irrational given that a character literal is of type "int" in C. (Although why it isn't of type "char" escapes me... ) The...
6
by: CFAN | last post by:
Here is a example of double-type-conversion char * __cdecl _itoa ( int val, char *buf, int radix ) { if (radix == 10 && val < 0) xtoa((unsigned long)val, buf, radix, 1); else
5
by: Peng Yu | last post by:
Hi, Please see the following code and the output. It seems that if one of %'s oprand is unsigned, the other will also be converted to unsigned. There is a operator precedence table in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.