-1

My thoughts: if one declares an int it basically gets an unsigned int. So if I need a negative value I have to explicitly create a signed int.

I tried

int a = 0b10000101;
printf("%d", a); // i get 138 ,what i've expected
signed int b = 0b10000101; // here i expect -10, but i also get 138
printf("%d", b); // also tried %u

So am I wrong that an signed integer in binary is a negative value?
How can I create a negative value in binary format?

Edit Even if I use 16/32/64 bits I get the same result. unsigned/signed doest seems to make a difference without manually shifting the bits.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    `int` ***is*** always `signed`, so there's no difference between `a` and `b` in your example. The most common way to represent signed integer types is *two's complement*, so please [read more about it](https://en.wikipedia.org/wiki/Two%27s_complement). – Some programmer dude Feb 02 '19 at 03:22
  • -10 in 2's complement is `leading ones... 11110110`. why do you expect `0b10000101` to be -10? – chux - Reinstate Monica Feb 02 '19 at 04:39

4 Answers4

1

Keep in mind that, depending on your target, int could be 2 or 4 bytes. This means that int a=0b10000101 is not nearly enough bits to set the sign bit.

If your int is 4 bytes, you need 0b10000000 0000000 0000000 00000000 (spaces added for clarity).

For example on a 32-bit target:

int b = 0b11111111111111111111111111111110; 
printf("%d\n", b);   // prints -2
Kon
  • 4,023
  • 4
  • 24
  • 38
  • See my updated example, make sure you have the right number of bits. Double count if necessary, it's easy to be off. – Kon Feb 02 '19 at 03:25
  • Oh ok i thought i dont have to do the binary operation (flip the bits) if i declare signed or unsigned? So why to use it? – StrangerThings Feb 02 '19 at 03:29
  • `int` is by default `signed`, you really only need to specify `unsigned`. also keep in mind that to print unsigned you need to use `%u`. – Kon Feb 02 '19 at 03:30
  • If you say "its signed by default" does it mean its "negative" by default? – StrangerThings Feb 02 '19 at 03:35
  • @StrangerThings it means that values can be negative or positive, unsigned means you only have positive numbers. –  Feb 02 '19 at 03:40
  • No, signed doesn't mean negative. Read more about [signed vs unsigned](https://stackoverflow.com/questions/247873/signed-versus-unsigned-integers) – Kon Feb 02 '19 at 03:41
1

because int a = 0b10000101 has only 8 bits, where you need 16 or 32. Try thi:

int a = 0b10000000000000000000000000000101

that should create negative number if your machine is 32bits. If this does not work try:

int a = 0b1000000000000101

there are other ways to produce negative numbers:

int a = 0b1 << 31 + 0b101

or if you have 16 bit system

int a = 0b1 << 15 + 0b101

or this one would work for both 32 or 16 bits

int a = ~0b0 * 0b101

or this is another one that would work on both if you want to get -5

int a = ~0b101 + 1

so 0b101 is 5 in binary, ~0b101 gives -6 so to get -5 you add 1

EDIT: Since I now see that you have confusion of what signed and unsigned numbers are, I will try to explain it as simple as possible int

So when you have:

 int a = 5;

is the same as:

 signed int a = 5;

and both of them would be positive. Now it would be the same as:

 unsigned int a = 5;

because 5 is positive number.

On the other hand if you have:

int a = -5;

this would be the same as

signed int a = -5;

but it would not be the same as following:

unsigned int a = -5;

the first 2 would be -5, the third one is not the same. In fact it would be the same if you entered 4294967291 because they are the same in binary form but the fact that you have unsigned in front means that compiler would store it the same way but treat it as positive value.

  • It doesnt work but ODYN-Kon's answer worked for me. But i expected the same like you.. Of course bit shifting works as expected but i asked about signed/unsigned usage. – StrangerThings Feb 02 '19 at 03:31
  • Your first four examples all involve undefined behavior as far as the C language is concerned. Also, C specifies the effect of assigning a negative value to an unsigned integer in a way that does not depend on representation. The result will *not* have the same representation on a sign/magnitude or one's complement machine -- both of which C explicitly allows -- not that you're likely to actually run into such a machine. – John Bollinger Feb 02 '19 at 03:56
  • @JohnBollinger so I got the second part and you are right. I completely forgot about that one. But why undefined behavior? the other thing is that I tent to simplify sometimes things for those who are beginners. –  Feb 02 '19 at 04:09
  • @Gox, constants are positive. Assigning a large positive constant to a signed integer so as to cause overflow produces undefined behavior. Similarly, a left shift of a signed integer (including a signed integer constant) whose mathematical result is out of range for the type also produces undefined behavior. – John Bollinger Feb 02 '19 at 04:56
  • @JohnBollinger thank you for your input. I did find out what you are saying. I can say I learned a bit here. –  Feb 02 '19 at 23:20
1

If numbers are represented as two's complement you just need to have the sign bit set to ensure that the number is negative. That's the MSB. If an int is 32 bits, then 0b11111111111111111111111111111111 is -1, and 0b10000000000000000000000000000000 is INT_MIN.

To adjust for the size int(8|16|64)_t, just change the number of bits. The sign bit is still the MSB.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
0

How to create a negative binary number using signed/unsigned in C?

Simply negate the constant of a positive value. To attempt to do so with many 1's ... 1110110 assumes a bit width for int. Better to be portable.

#include <stdio.h>

int main(void) {
  #define NEGATIVE_BINARY_NUMBER (-0b1010)
  printf("%d\n", NEGATIVE_BINARY_NUMBER);
}

Output

-10
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • what does this have to do with signed/unsigned statement? – StrangerThings Feb 02 '19 at 20:26
  • @StrangerThings `0b1010` is an integer constant of type `signed int`. The assignment to an `int` variable like `signed int b = some_value` is not necessary to achieve "How to create a negative binary number". `-0b1010` itself is a "negative binary number". – chux - Reinstate Monica Feb 02 '19 at 20:32