473,387 Members | 1,535 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,387 software developers and data experts.

Unsigned Short Int

How is this code giving the correct answer? I though unsigned short int is only positive numbers. I am giving it a -3 and I am getting 0 for sum.

thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef unsigned short int Ushort;
  6.  
  7. int main()
  8. {
  9.     Ushort x = -3;
  10.     Ushort y = 3;
  11.  
  12.     Ushort sum = x+y;
  13.     cout << sum << endl;
  14.  
  15.     cin.get();
  16.     return 0;
  17. }
Jul 3 '10 #1

✓ answered by hype261

@Deanm007
The reason you are getting 0 for the sum is because when you set UShort x = -3 that is actually being set to the max value of a unsigned short int minus 3. Which on my system is 65533. Then you are adding 3 to it which is overflowing the unsigned short int which sets it back to 0.

1 12023
hype261
207 100+
@Deanm007
The reason you are getting 0 for the sum is because when you set UShort x = -3 that is actually being set to the max value of a unsigned short int minus 3. Which on my system is 65533. Then you are adding 3 to it which is overflowing the unsigned short int which sets it back to 0.
Jul 3 '10 #2

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

Similar topics

4
by: James Roberge | last post by:
I am having a little trouble getting my union/struct to work correctly. I am creating a struct that will contain information about the status of various Z80 cpu registers in an emulator i am...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
10
by: Vijay Kumar R Zanvar | last post by:
Hi clc, Please elaborate the warning: F:\Vijay\C> type unsigned2.c #include <stdio.h> #include <stdlib.h> int
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
1
by: wolverine | last post by:
Hi, Given below is a traits with unsigned short. If i create a stringstream with this traits will there be any problem. struct XMLCh_traits { typedef unsigned short char_type; typedef...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
2
by: CptDondo | last post by:
I'm trying to write what should be a simple program, and it keeps hanging if I use volatile.... The program, stripped of its error checking, is this: unsigned short * start; unsigned short *...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.