[关闭]
@ghostfn1 2016-04-23T21:06:39.000000Z 字数 1591 阅读 1470

C++ Unsigned and Signed Integers

C++


Update Time:160423 Saturday

  1. In computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits. The size of the grouping varies so the set of integer sizes available varies between different types of computers. —— Wikipedia
  2. An unsigned integer containing n bits can have a value between 0 and $ 2^n-1 $ (which is $2^n$ different values). 8 bits, called a "byte""(short integers" (16 bits), "long integers" (32 bits) or "double integers" (64 bits). If a signed integer has n bits, it can contain a number between - 2^n - 1 and + (2^n - 1 - 1).
  3. e.g. The BIOS (Basic Input Output Software) in older PCs uses 10 bits to store the cylinder number on the hard drive where your operating system begins; therefore those PCs cannot boot an operating system from a cylinder greater than 2^10-1, or 1023.
  4. "32-bit software" is restricted to 232 virtual memory addresses per process, while "64-bit software" has 264 (18,446,744,073,709,551,616) addresses available.
  5. When you get to 127, the integer has a value of 0 1 1 1 1 1 1 1(2); this is easy to see because you know now that a 7 bit integer can contain a value between 0 and 2^7 - 1, or 127. What happens when we add 1?
  6. Note that the most negative number which we can store in an 8 bit signed integer is -128, which is -2^(8-1), and that the largest positive signed integer we can store in an 8 bit signed integer is 127, which is 2^(8-1)-1.
  7. The number of integers between -128 and + 127 (inclusive) is 256, which is 28; this is the same number of values which an unsigned 8 bit integer can contain (from 0 to 255).
  8. 1 0 0 0 0 0 0 1(2) is equivalent to -127.

References:
Unsigned and Signed Integers
C++ Integer

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注