Can Some one explain why I get these output? [duplicate] - c

This question already has answers here:
What is “two's complement”?
(24 answers)
Closed 9 months ago.
I am new to c.
My Question.
Why do some numbers end up negative when the value was positive?

Here is an explanation for the output you are seeing.
1000 in binary is 1111101000 (10 bits) and is stored in an int (signed 32 bits)
When you cast that to an unsigned char (that has 8 bits), the top bits get "cut off".
So you get: 11101000 which is 232 in decimal.
As a (signed) char, the bits get interpreted as a negative number because the first (sign) bit is set, which in this case is -24.
When you remove the sign bit, 1101000 = 104
The "value" of the MSB is 128, so your computer does 104 - 128 = -24.
(See https://en.wikipedia.org/wiki/Two%27s_complement)
A long has the same or more bits as an int so the value does not change.

Related

Bitwise operator gave me a negative number [duplicate]

This question already has answers here:
C bitwise negation creates negative output: [duplicate]
(3 answers)
Closed last year.
I just learned. How Bitwise operators work but when i try to use it in the c code i doesnt work.
#include <stdio.h>
int main()
{
int a = 7;
printf("%d\n", (int)~a);
return 0;
}
The expected output is 8 but it comes -8.
~0111
=
1000
Assuming int is 32 bit on your machine then 7 is 0b00000000000000000000000000000111 and ~7 becomes 0b11111111111111111111111111111000 which is -8.
Background
For signed values the most significant bit is used to determine if the value is negative or not. When the MSB is set, the value is negative.
In addition to that (char) 0b10000000 is -128 and (char) 0b11111111 is -1.
So counting works as follows:
0b10000000 0b10000001 [...] 0b11111111 0b00000000 [...] 0b01111111
-128 -127 [...] -1 0 [...] 127
That is the reason why you will get -128 when you count 127+1 and try to store that in a char.

What happens when I apply the unary "-" operator to an unsigned integer? [duplicate]

This question already has answers here:
Assigning negative numbers to an unsigned int?
(14 answers)
Closed 6 years ago.
This should be a pretty simple question but I can't seem to find the answer in my textbook and can't find the right keywords to find it online.
What does it mean when you have a negative sign in front of an unsigned int?
Specifically, if x is an unsigned int equal to 1, what is the bit value of -x?
Per the C standard, arithmetic for unsigned integers is performed modulo 2bit width. So, for a 32-bit integer, the negation will be taken mod 232 = 4294967296.
For a 32-bit number, then, the value you'll get if you negate a number n is going to be 0-n = 4294967296-n. In your specific case, assuming unsigned int is 32 bits wide, you'd get 4294967296-1 = 4294967295 = 0xffffffff (the number with all bits set).
The relevant text in the C standard is in §6.2.5/9:
a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type
It will overflow in the negative direction, i.e. if your int is 16 bits x will be 65535. The bit value will be 1111111111111111 (16 ones)
If int is 32 bits, x will be 4294967295
when you apply the "-", the Two's complement of the integer is stored in variable. see here for details

Computable Size of Integer [duplicate]

This question already has answers here:
What is “two's complement”?
(24 answers)
Closed 7 years ago.
An int32 is represented in computer memory with a size of 4 bytes (32 bits).
So, 32 bits have 1 sign bit and 31 data bits. But if 1st bit starts at 2^0, then the 31st bit should have 2^30, and the last bit is of course the sign bit.
How is it then that integer extends from -2^31 to (2^31)-1?
So, 32 bits have 1 sign bit and 31 data bits.
No. Most platforms use two's complement to represent integers.
This avoids double zero (+- 0) and instead extends the range of negative numbers by 1. The main advantage is in arithmetic: Many operations, like addition and subtraction can simply ignore the sign.
An int32 has exactly 32 bits, and can hold 2^32 different values.
In unsigned form, these will be 0 -> (2^32)-1.
In signed form, these will be -2^31 -> (2^31)-1. Notice that:
(0 - (-2^31)) + ((2^31)-1 - 0) =
2^31 + 2^31 - 1 =
2*2^31 - 1 =
(2^32) - 1
Exactly the same range.

How to understand the incremented unsigned value concept? [duplicate]

This question already has answers here:
Wrap around explanation for signed and unsigned variables in C?
(4 answers)
Closed 8 years ago.
int i=~0;
uint j=(uint)i;
j++;
printf("%u",j);
I am slightly confused as before increment j is "4294967295", but after increment(j++) instead of becoming "4294967296" it is 0...can anyone please explain?
The range of 32-bit unsigned int is
0 to 4,294,967,295
So incrementing beyond this value(like +1 to this value) will lead to rolling back/wrap around to 0.
Edits:
§6.2.5 Types ¶9 A computation involving unsigned operands can never
overflow, because a result that cannot be represented by the resulting
unsigned integer type is reduced modulo the number that is one greater
than the largest value that can be represented by the resulting type
It's because it overflows, meaning the data type goes over the maximum value it can represent.
int i = ~0
All bits are set to 1. For an int, this is interpreted as -1.
uint j=(uint)i;
You copy the data and convert it to unsigned int. -1 can't be represented by an unsigned int and will similar to below wrap around so it also has all its bits set to 1.
j++;
When you add by one it overflows. It's easy to see why if you look at the addition in bits. The number is only represented by a certain number of bits, on your machine a int is 32-bit. For a 4-bit number it would look like this:
1111 + 1 = 10000
But the highest order bit has nowhere to be stored, for a unsigned integer this is defined to wrap around like this:
1111 + 1 = 0000

How can 256 be represented by a char? [duplicate]

This question already has answers here:
what is char i=0x80 and why overflow did not happen in bit shifting
(6 answers)
Closed 9 years ago.
I ran the following code in xcode in to my surprise the answer was 256.
Since char is only 8 bits long i expected this to be 0.
Dumping the 1 in the 8 place.
Can someone explain what is going on?
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
It is being promoted to an integer, which can contain the value of 256 just fine. A cast to unsigned char will give you the result you expected:
printf("%d\n", (unsigned char)(i<<1) );
It is being promoted to an integer when you do i << 1. For example, SHL works on 32 bit words (even though your c type is 8 bit) in x86 so the shifted bit isn't discarded out of the 32 bit register. When you print this region with with "%d", you get 256. If you want to left shift with unsigned char, you can always do (i << x) & 0xff.
In this case, what is happening is that the intermediate result of i<<1 is an integer, which can represent 256.

Resources