-1

I already know the concept of negative binary numbers, The 0 at the position most significant bit represents that the binary is positive and 1 at the position of most significant bit represents that the binary is negative.

BUT THE PROBLEM THAT INTIMIDATED ME TO ASK A QUESTION ON STACKOVERFLOW IS THIS: what about the times that we might want to represent a huge number that it's representation has occurred to have 1 in msb.

let me explain it in this way: by considering the above rule for making negative counterparts of our binary numbers we could say that ; in an 8-bit system we have, For example, a value of positive 12 (decimal) would be written as 00001100 in binary, but negative 12 (decimal) would be written as
10001100 but what makes me confused a bit is that 10001100 could also be interpreted as 268 in decimal while we know that its the negative form of 12 in binary using this method of conversion.

I just want to know how to deal with this tricky, two-faced possible ways of interpreting a binary number, just like the example i gave above(it seem's to be negative, OH! but wait it might also not be:).

1 Answers1

0

It depends on the type you use. If you're using an 8-bit representation which is signed, then the largest number you can store is 1111111 (i.e. the first bit is set aside).

In our example, that would convert to an integer value of 127.

If you use an unsigned type, then you have the extra bit available, allowing you to store 11111111, or 255 expressed as an integer.

A strongly typed language with a good compiler should catch you trying to assign, say, 134 to a signed 8 bit integer and vomit errors all over you.

If you're doing something strange fiddling around with bits yourself, you're on your own! There's no way of reconstructing, post hoc, whether it was intended to be a negative or a large positive, so you'll have to choose a system and stick with it.

The general convention nowadays is to stick with signed representations always - although I have seen code (usually for extreme compute tasks like astrophysical calculations) use unsigned values simply to save memory. And of course images will use unsigned values by convention, usually.

thclark
  • 4,784
  • 3
  • 39
  • 65
  • the problem is that, I myself know that msb is my sign and its a negative binary, but when i convert it using online converters, it seems a little tricky, so how do computers understand that this is a negative binary, because I have tried a website to convert it, and it didn't recognized that this is a negative binary and converted 10001100 to 268 –  Nov 01 '19 at 14:46
  • 1
    You'll need to choose an online converter which allows you to tell it whether the type is signed or not. Also one that's correct, because even as an unsigned value, that should be 140!! This converter understands signed numbers: https://binary-system.base-conversion.ro/convert-from-signed-binary-to-integers.php – thclark Nov 01 '19 at 14:49
  • what about python, how does it deal with it ? –  Nov 01 '19 at 14:53
  • 1
    @moh88 perhaps it would be helpful if you could edit your question to be clearer about what you have, and what you're trying to achieve ultimately? That'll save a lot of comments pingpong – thclark Nov 01 '19 at 14:56
  • sorry for that this is the last question. –  Nov 01 '19 at 14:57
  • 1
    Here's how python deals with type conversion. https://www.datacamp.com/community/tutorials/python-data-type-conversion If you'd like more targeted help, perhaps put up a question with an example of the data that you have, specifying that what you need to do with it :) – thclark Nov 01 '19 at 14:59