简体   繁体   English

C ++如何代表负值

[英]How does c++ represent negative value

As i know binary equivalent of signed int is 11111111111111111111111111111111 据我所知,有符号整数的二进制等效项是11111111111111111111111111111111111
and based on this, I'm trying to make Maximum and Minimum int value for my program without using limits.h header file. 基于此,我试图在不使用limits.h头文件的情况下为程序创建maximum和Minimum int值。 After run my below code i get Minimum value as -2147483648 and Maximum value is 0. Here below is my code: 运行下面的代码后,我得到最小值为-2147483648,最大值为0。这是我的代码:

int MaxInt(){
      int MAX = -1;
      MAX = 0 << ((sizeof(int)*8)-1);

     return MAX;
}


int MinInt(){
      int MIN = 0;
      MIN  = 1 << ((sizeof(int)*8)-1);

     return MIN;
}

Whats wrong with my implementation. 我的实现有什么问题。

In the function 在功能上

int MaxInt(){
      int MAX = -1;
      MAX = 0 << ((sizeof(int)*8)-1);

     return MAX;
}

you at first assigned -1 to MAX and then overwrote its value. 您首先将-1分配给MAX,然后改写了它的值。 So this assignment does not make sense. 因此,此分配没有意义。

Also if to shift left 0 then you will get again 0 independing on how long you will shift the 0.:) 同样,如果向左移动0,那么您将再次获得0,具体取决于您将0移动多长时间。:)

The simplest way to get the maximum value of object of type int for the 2's complement internal representation is the following 获取2的补码内部表示形式的int类型的对象的最大值的最简单方法如下

int MaxInt()
{
    int MAX = -1u >> 1;
    return MAX;
}

Or you can write simply 或者你可以简单地写

int MaxInt()
{
    return -1u >> 1;
}

Here is a demonstrative program 这是一个示范节目

#include <iostream>

constexpr int MaxInt()
{
    return -1u >> 1;
}

constexpr int MinInt()
{
    return ~( -1u >> 1 );
}

int main()
{
    std::cout << MaxInt() << std::endl;
    std::cout << MinInt() << std::endl;
} 

The program output might look like 程序输出可能看起来像

2147483647
-2147483648   

Your implementation has several mistakes: 您的实现有几个错误:

First, your representation of -1 assumes that int has a twos-complement 32 bit representation. 首先,您对-1的表示假设int具有二进制补码的32位表示。 This is not guaranteed for int . 对于int不能保证。 (It is for std::int32_t .) (它用于std::int32_t 。)

Second, you assume that int has sizeof(int)*8 bits. 其次,假设int具有sizeof(int)*8位。 This again is not guaranteed at all. 再次完全不能保证。

Under all these assumptions, you still have a mistake in your implementation: 在所有这些假设下,您在实现中仍然有一个错误:

0 << ((sizeof(int)*8)-1);

can be written (mathematically, not in c++) as: 可以写成(数学上,不是用C ++):

0 * 2**((sizeof(int)*8)-1)

Now, as you know, multiplying something with 0 results in 0 . 如您所知,现在将某物与0相乘将得到0

Assuming that twos-complement is given, the following simple implementations should work: 假设给出了二进制补码,则以下简单的实现应起作用:

MIN = -1 << ((sizeof(int)*8)-1);
MAX = ~MIN;

Whats wrong with my implementation 我的实施有什么问题

MAX = 0 << ((sizeof(int)*8)-1);

Shifting zero by any amount will always be zero. 将零移位任何量将始终为零。

This isn't specific to C++, but rather about 2's complement form. 这不是特定于C ++,而是2的补码形式。 In 2's complement, the most-significant bit doesn't merely indicate the sign (that the value is negative) but is rather that power of 2 (that is, for an 8-bit 2's complement number, the most significant bit would represent -2^7). 在2的补码中,最高有效位不仅指示符号(该值是负数),而且还指示2的幂(即对于8位2的补码,最高有效位将表示- 2 ^ 7)。

To make the most negative number, only the most significant bit should be set. 要设置最大负数,应仅设置最高有效位。

  // Disclaimer: this should work for *most* devices, but it
  // is device-specific in that I am assuming 2's complement
  // and I am also assuming that a char is 8-bits. In theory,
  // you might find a custom chip where this isn't true,
  // but any popular chip will probably have this behavior:

  int number_of_digits_in_int = sizeof(int) * 8;
  int most_significant_digit_index = number_of_digits_in_int - 1;
  int most_negative_int = 1 << most_significant_digit_index;

To make the largest positive number, all positive bits should be set: 为了获得最大的正数,应设置所有正数位:

  // The complement of 0 has all bits set. This value, by the way
  // is the same as "-1" in 2s complement form, but writing it
  // this way for clarity as to its meaning.
  int all_bits_set = ~(static_cast<int>(0));

  // Using an XOR with the most negative integer clears the
  // most-signficant sign bit, leaving only positive bits.
  int largest_positive_int = all_bits_set ^ most_negative_int;

Or, more simply: 或者,更简单地说:

  // Since the most negative integer has only the negative bit set,
  // its complement has only the positive bits set.
  int largest_positive_int = ~most_negative_int;

As others have stated, though, you should just use std::numeric_limits . 但是,正如其他人所述,您应该只使用std::numeric_limits This will also make your code portable and work even on very bizaare devices that don't use 2s complement, for example, not to mention that the less code you write yourself, the fewer mistakes that you'll make. 这也将使您的代码可移植,甚至在不使用2s补码的非常笨拙的设备上也可以工作,例如,更不用说您编写的代码越少,所犯的错误就越少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM