473,587 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between unsigned int and signed int

1 New Member
what are major differences between signed and unsigned int
Mar 19 '14 #1
4 3352
weaknessforcats
9,208 Recognized Expert Moderator Expert
An unsigned int cannot be negative.
Mar 19 '14 #2
Banfa
9,065 Recognized Expert Moderator Expert
If you add 1 to a signed int that already has it's maximum positive value you produce undefined behaviour.

If you add 1 to an unsigned int that already has it's maximum positive value it wraps round to a value of 0.
Mar 19 '14 #3
Sherin
77 New Member
A signed data type uses the first bit to indicate whether it is a positive/negative number. An unsigned data type does not store it’s sign and is usually used for number guaranteed to be positive/negative (such as elapsed time).

An unsigned short (2 bytes) can represent values from 0 to 2^16, while a signed data type can store values form -2^15 to 2^15.

Signed data types can hold both positive and negative values.

Unsigned data types can hold large positive values but cannot hold negative values.

let take an example in c language:

signed int negative=-11; // you can store negative value here//
unsigned int number=11; // here you can only store positive number//
Aug 17 '20 #4
Banfa
9,065 Recognized Expert Moderator Expert
A signed data type does not use the first bit to indicate if it is positive or negative, that would be 1's complement, most computers represent signed values as 2's complement.

Using an 8 bit signed integer then

1's complement
1 = 00000001
-1 = 11111110

2's complement
1 = 00000001
-1 = 11111111

In 2's complement you can just do a bit wise addition to add 2 numbers

i.e. 1 + -1 = 00000001 + 11111111 = 00000000 = 0

In 1's complement you have to process first because a bit wise addition of 00000001 and 11111110 would be 11111111 which equals -0.

It is one of the reasons for using 2's complement another being in 1's complement there is 2 representations of 0 00000000, 11111111 i.e. +0 or -0 which also tends to complicate things.

It is true that the MSB does indicate if the value is negative but that is a side effect of the represenation of numbers in 2's compliment rather than the identifying feature of the representation.

The range of a signed (2 byte) value is not -2^15 to 2^15 it is -2^15 to 2^15-1. This can lead to poblems if you are using the limits of the value as it can hold a larger negative number than positive number.
Aug 18 '20 #5

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

Similar topics

19
6466
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler...
4
2648
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO lay out what should happen for: -------------------------- signed char sc; unsigned char uc;
5
20595
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 conversion happens? Also on 32 and 64bit machines what happens? Can anybody explain?
10
15633
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a...
20
5331
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
3
4225
by: Jon B | last post by:
Hi There! What is the difference between Signed and UnSigned variables in .NET Framework?? My background is just in VB6 and I have no idea what is the difference between ULong and Long etc. Please enlighten me!! Thanks!! Jon
1
2555
by: kunal patil | last post by:
Is there any significant difference (apart from range) in signed char and unsigned char in 'C'. wt can be an application of signed char??
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.