-3

we know if we want to store num=-1 in 32 bit signed int its bits representation will look like 1000 0000 0000 0000 0000 0000 0000 0001

but if I do like this num=-1 unsigned int num1 = num then num1 bit representation will look like 1111 1111 1111 1111 1111 1111 1111 1111.

we also know that in unsigned int msb does not represent sign ,it contributes to magnitude ,I want to know why there are all ones in num1 representation how can I predict the value for -2 in unsigned int

Note I was trying all the operation in cpp language

#include<bits/stdc++.h>
using namespace std;
int main(){
    unsigned int num = -1;
    ///i want to know what will be the bit representation of num=-2
    while(num){
        cout<<(num&1);
        num>>=1;
    }

}
dayok
  • 1
  • 4
    regarding your first sentence, perhaps you should try inspecting the representation on your system – M.M Jul 24 '20 at 04:34
  • 1
    They're both stored as all 1's. Google "Two's complement" – Delta_G Jul 24 '20 at 04:34
  • but suppose in 8 bit we represent 1 as 0000 0001 and to get -1 first we will do 1s complement 1111 1110 and then two complement like this 1111 1111 – dayok Jul 24 '20 at 04:39
  • No, that's not how signed integers work on almost all modern systems. -1 is all ones. – eesiraed Jul 24 '20 at 04:39
  • 1
    Recommended readings: [Why you shouldn't `#include `](https://stackoverflow.com/q/31816095/9254539) and [Why `using namespace std` is considered bad practice](https://stackoverflow.com/q/1452721/9254539). – eesiraed Jul 24 '20 at 04:42
  • Standard unsigned types in C++ use modulo (aka "wrap around") arithmetic. So storing `-1` to an `unsigned` type gives the largest *numerical* value that type can represent. For a 32-bit unsigned type, that is `4294967295`. Which will have an "all bits set" representation. A value of `-2` will be stored as `4294967294`, which will have all bits except the last set. Bear in mind that an `unsigned` is not guaranteed to be 32 bit (it can be 16 bit, since the maximum value it can represent is guaranteed to be at least `65535`). – Peter Jul 24 '20 at 04:43

2 Answers2

0

As you said, the MSB of unsigned ints contributes to magnitude, as do all the other bits. You can see since unsigned int num1 = -1 gives 1111 1111 1111 1111 1111 1111 1111 1111 which is the greatest possible unsigned value (2^32). -2 is literally num1 - 1. Therefore you can just subtract 1 and get 1111 1111 1111 1111 1111 1111 1111 1110 (2^32 - 1).

https://www.tutorialspoint.com/two-s-complement

0

When you assigned a negative number to unsigned int the negative number as you said the first bit is sign bit and the other is binary of the number for example binary representation -2 is 1000 0000 0000 0000 0000 0000 0000 0010 unsigned int get two's complete of this number .If you want to convert the signed binary to two's complete from first 1 from right every bit get not except for signed bit so two's complete of -2 should be 1111 1111 1111 1111 1111 1111 1111 1110.

Farhad Sarvari
  • 1,051
  • 7
  • 13