Overflow of arithmetic operations and a number in memory represents the number of 1

Source: Internet
Author: User
# Include <iostream> using namespace STD; int numonesinbinary (INT num) // counts the representation of a number in the memory. There are several 1 {int numones = 0; while (num! = 0) {num = num & num-1; numones ++;} return numones;} void main () {int A =-1; unsigned int B = 2; cout <(unsigned INT) A <Endl; cout <a + B <Endl; A =-3; // [if you have any questions,] cout <a + B <Endl; // The result is unsigned int} {short a =-1; unsigned short B = 2; cout <(unsigned short) A <Endl; cout <a + B <Endl; A =-3; // [Have questions here] // enter the question and above Code Is the output inconsistent when it is int? Cout <a + B <Endl; // Why not 65535? Isn't the result after a + B an unsigned number? } Int a1 =-1; int a2 = 88; cout <(A1 & A2) <Endl; // bitwise AND, -1 memory is represented as 32 1 int ival =-1; cout <"The num of 1 in momory is:" <numonesinbinary (ival) <Endl; {// The unsigned number arithmetic operation does not overflow: Unsigned short b1 = 65530; unsigned short b2 = 5000; cout <"B1 + b2 =" <B1 + b2 <Endl ;}{// when calculating addition, the signed short is promoted to unsigned short first, so there is no overflow saying that unsigned short b1 = 65530; short b2 = 5000; cout <"B1 + b2 =" <B1 + b2 <Endl ;} {// overflow short b1 = 65530; short b2 = 5000; cout <"B1 + b2 =" <B1 + b2 <Endl ;}getchar ();}


1) The numonesinbinary function is applicable to positive and negative numbers.

2) [if you have any questions here] I don't understand. The result of the unsigned number and the signed number arithmetic operation should be the unsigned number. Why is there an output-1? If you happen to see this article, please kindly advise.

3) & bitwise AND, that is, bitwise and in the memory representation. Any number and-1 are bitwise AND, and the result is itself.

Overflow of arithmetic operations:

There are two types of integer arithmetic operations in C language: signed and unsigned.

In unsigned arithmetic operations, there is no such thing as "overflow ".: All unsigned operations are modeled on the N power of 2, where N is the number of digits in the result.

If one operand of an arithmetic operation is a signed integer and the other is an unsigned integer, the signed integer is converted to an unsigned integer before the operation, and "overflow" is impossible.

When both operands are signed integers, "overflow" may occur.


Assuming that A and B are two non-negative integer variables, how can we check whether a + B overflows?

If(A+B<0)// ErrorComplain();If((Unsigned)A+ (Unsigned)B>Int_max)// OK, where is int_max? <Limits. h>Complain();If(A>Int_max-B)// OKComplain();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.