How does c++ represent negative value

14,552

Solution 1

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. 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.:)

The simplest way to get the maximum value of object of type int for the 2's complement internal representation is the following

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   

Solution 2

Your implementation has several mistakes:

First, your representation of -1 assumes that int has a twos-complement 32 bit representation. This is not guaranteed for int. (It is for std::int32_t.)

Second, you assume that int has sizeof(int)*8 bits. 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:

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

Now, as you know, multiplying something with 0 results in 0.

Assuming that twos-complement is given, the following simple implementations should work:

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

Solution 3

Whats wrong with my implementation

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

Shifting zero by any amount will always be zero.

Solution 4

This isn't specific to C++, but rather about 2's complement form. 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).

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. 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.

Share:
14,552
maruf
Author by

maruf

Updated on June 04, 2022

Comments

  • maruf
    maruf almost 2 years

    As i know binary equivalent of signed int is 11111111111111111111111111111111
    and based on this, I'm trying to make Maximum and Minimum int value for my program without using limits.h header file. After run my below code i get Minimum value as -2147483648 and Maximum value is 0. Here below is my code:

    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.