473,406 Members | 2,713 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,406 software developers and data experts.

Typecasting of a unsigned value to signed integer

Hello,

For the snippet below
Expand|Select|Wrap|Line Numbers
  1. unsigned int tmp1=65535;
  2. unsigned int tmp2=0,tmp3=0;
  3. tmp2=(char)tmp1;
  4. tmp3=(int)tmp1;
  5. printf("tmp1=%u\ntmp2=%u\ntmp3=%u\n",tmp1,tmp2,tmp3);
The output of the code was
tmp1=65535
tmp2=255
tmp3=65535


The binary value of 65535 is 1111 1111 1111 1111.


So when we do tmp2=(uint8)tmp1, the upper 8 bits are not considered as it is typecasted to uint8.
So I understand tmp2 is assigned 255.

But when we do tmp3=(sint8)tmp1 ,I dont know how the output of 65535 is produced?
(sint16)1111 1111 1111 1111===>0000 0001(2's complement of 1111 1111).

when this is assigned to tmp3 the output should have been 255.I dont understand how 65535 comes.

I guess the calculation goes as follows:
1111 1111 ==============================>0000 0001
2's complement of lower byte

0000 0001=================>0000 0000 0000 0001
(32 bit notation)

0000 0000 0000 0001===========>1111 1111 1111 1111
2's comp


[But I dont understand why the last 2's complement calculation is carried out.The above 3 calculations are just my ideas.If this idea is wrong please let me know where I am going wrong]
Jan 21 '15 #1
4 6575
weaknessforcats
9,208 Expert Mod 8TB
I'm just about ready to write a C++ Insight on the reasons why you never cast. This is one example.

First, casting changes nothing. The compiler flags an error because types don't match and rather than code around the error, the programmer does a typecast which tells the compiler to forget about the rules and stop putting out this error. It's based on the supposition that the programmer knows more about what's going on than the compiler does.

In this case, an unsigned integer cannot be cast to a signed integer since the left bit is data whereas in a signed integer the left bit is the sign bit. Further, if the sign bit is ON then the data is usually in 2's complement format since it is a negative number.

Therefore, the bits change their value between the two formats.

Since there's no way to control the value in the unsigned int without doing extra code to insure the left bit is always OFF, the typecast is very ill-advised.

Can you not just use a signed int?
Jan 21 '15 #2
donbock
2,426 Expert 2GB
I don't recognize uint, uint8, or sint8. I'll assume that these are equivalent to unsigned int, unsigned char, and signed char respectively.

Line 3 converts a 16-bit unsigned value into an 8-bit unsigned value and then assigns that result to an unsigned int. The C Standard requires that the result of the first conversion be the original value mod 2^8; so this result can be counted on to be 255. 255 can be represented by an unsigned int so the assignment doesn't change anything,

Line 4 converts a 16-bit unsigned value into an 8-bit signed value and then assigns that result to an unsigned int. The first of these conversions is undefined because the original value cannot be represented with an 8-bit signed value, so don't waste your time trying to make sense out of what happens. (The C Standard has different rules for when the destination type is signed or unsigned.)
Jan 21 '15 #3
hello,
Sorry i gave the wrong syntax.I understand your point.It was a snippet to just get some basic idea abou type casting.
Jan 22 '15 #4
Hello,

I corrected the syntax now.Thanks for your reply
Jan 22 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
1
by: b83503104 | last post by:
When are they not consistent?
3
by: codefixer | last post by:
Hi, I was wondering what will be the output for this piece of code. I am in confusion regarding the promotions of bitfield. If your reply is to compile, execute and check out myself, Thank you...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
19
by: junky_fellow | last post by:
How the unsigned to signed integet conversion is done ? For eg: unsigned int ui = 100; int si = ui; What will be the value if "si" is printed ? How this conversion is done ? Thanx for any...
27
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
4
by: Ioannis Vranos | last post by:
Under C95: Is it guaranteed that char, unsigned char, signed char have no padding bits?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.