Using Bitwise in C, return second smallest two's complement integer - bit-manipulation

I'm pretty new to bit manipulations and bitwise so I'm struggling to understand what this question is asking.
What number would be the second smallest twos complement integer?
Or what number would be the smallest? Does it depend on the computer? Or is it standard for each computer?

Related

One's complement adder

I am not sure this is the proper section of the forum, in case please just let me know.
I am studying Computer Organization and Design, and the legendary Patterson & Hannessy states that "One's complement adders did need an extra step to subtract a number and hence two's complements dominates today".
What is the extra operation that does an one's complement require?
The two's complement adder just straight up adds two numbers, bit by bit. The clever bit is that negative numbers are represented in such a way that signed and unsigned addition use exactly the same algorithm, whereas with one's complement you need a runtime check in the signed adder (negative inputs need an extra +1).
In other words, when you're doing 5 + -2 on a 4-bit adder, the two's complement adder gets 0101 and 1110 as inputs and it doesn't need to care whether 1110 represents -2 or 14.

Subtracting Hex using 2's Complement

i was wondering how would i go about subtracting two hex values using 2's Complement arithmetic? I know how to convert dec to binary and hex using the 2's complement, but im having trouble understanding how to subtract. Lets say we have the example below, how would i go about solving this using 2's complement without converting to bin or dec (if possible):
3A43 h - CB4A h
first of all: there is no 2's compliment in any base other than binary.
I never heard of a name for the hex pendant, but since 2 is the first number which is not available in binary I would call it G's or 10's complement.
the G's complement of CB4A:
10000
-CB4A
_____
34B6
do not forget that you can expand this number with Fs to the left.
3A43
FFFF34B6
________
FFFF6EF9
so the result is FFFF6EF9 in G's complement, which can be calculated to -9107 in sign-magnitude hex.

Definition of Two's Complement?

I've been reading over a couple of questions/answers here:
twos-complement-in-python
is-twos-complement-notation-of-a-positive-number-the-same-number
Someone gave some sample code to create a two's complement of a number:
def twos_comp(val, bits):
"""compute the 2's compliment of int value val"""
if( (val&(1<<(bits-1))) != 0 ):
val = val - (1<<bits)
return val
In addition someone defined two's complement as thus:
Two's complement notation uses the n-bit two's complement to flip the
sign. For 8-bit numbers, the number is subtracted from 2^8 to produce
its negative.
These declarations went unchallenged. However, that chides with my understand of a two's complement is. I thought it's calculated by inverting the binary number and adding 1. (With the understanding that the number representation has a limited number of bits.)
Additionally, the two's complement is supposed to have the property that it is the additive inverse of the original number. But, the output from twos_comp doesn't appear to have that. In my hand calculations (and some test code I wrote) with my definition , I see that when a number and the twos complement of it are added together, a 1 is overflowed and the rest of the bits are zero, thus it has the additive inverse property.
Are there multiple definitions for twos complement, am I confused, or is that definition and function from the other posts just plain wrong?
Twos complement is in fact calculated by inverting the binary number and adding 1, for negative numbers. Such that abs(-1)=1=01 -> bitwise_inv(abs(-1))+abs(-1)=FE+1=FF. This is equivalent to the definition provided of subtracting the number from 2^8 (this should not be hard to see).
The sample code you provided, does not calculate twos complement in any useful way. I don't at all understand whats its trying to do, appears to quite be quite different from "subtracting the number from 2^8" as subtracts 2*8 from the number, while also failing to remember that when we refer to the value of a twos complement number what we mean is its unsigned value.
Here is a more correct implementation, using the same template. Notice that this precisely does "subtract the number from 2^8."
def twos_c(val,bits):
if ((val&(1<<(bits-1)))!=0):
val=(1<<bits)-abs(val)
return val

Two's Complement disadvantage?

I was reading about two's complement, I understand this method is most efficient, but there might be some disadvantages too. I could not find any disadvantages, Is there any situation where the conversion to two's complement could fail to represent the number correctly?
Two's complement is awesome - that's why everyone uses it. The biggest disadvantage is that if you try to negate the lowest representable value, you get an overflow. With one's complement or sign and magnitude, that doesn't happen.
With "two complement"-notation you can't compare the size of two Integers with very simple logic operators (at lowest level hardware). Thats the reason why the exponent in IEEE Standard for Floating-Point Arithmetic (IEEE 754) is not represented in "two complement" but in "biased" notation.

Why is ~3 equal to -4 in Python?

I'm getting started in Python programming. I'm reading a basic tutorial, but this point is not very clear to me. I would appreciate any help you can give me.
~3 means 'invert' 3. With two's complement on natural number datatypes, this becomes -4, as the binary representation is inverted (all bits are flipped).
~3 means "change all the 1s to 0s and 0s to 1s", so if 3 in binary is 0000000000000011, then ~3 is 1111111111111100. since the first bit of ~3 is a 1, its a negative number. to find out which negative number, in 2s comliment, you invert all bits and add 1, so inverted we are back to 3, then added 1 we get 4.
Because signed integers are usually stored using two's complement, which means that the bitwise inverse of an integer is equal to its algebraic inverse minus one.
It's the invert operator, and returns the bitwise inverse of the number you give it.
It's not just Python, it's the integer numeric representation of almost all modern computers: two's complement. By the definition of two's complement, you get a negative number by complementing the positive number and adding one. In your example, you complemented with ~ but did not add one, so you got the negative of your number minus one.