Convert from base 10 to an arbitrary radix with proper sign extend - base-conversion

I was wondering if there's a formal way to properly sign extend base-10 numbers in an arbitrary base when converting. For example, if I had -256 in base 10, how would I properly sign extend the result in base 7 (or base n) without assuming a fixed length for the result.

From wikipedia:
The radix complement of an n digit number y in radix b is, by definition, bn − y
https://en.wikipedia.org/wiki/Method_of_complements#Numeric_complements
When we sign-extend the number to n+1 digits, the new representation for the value is
bn+1 − y = b*bn − y = bn − y + (b-1)bn
As b-1 is the largest digit in base b and bn contains all 0s in the n least significant digits (i.e. 100..0 with n zeros in base b), (b-1)bn is simply a number with b-1 followed by n zeros. The remaining part (bn − y) is the old n-digit radix complement in base n
So basically a 1-digit sign-extension in base b is just about prepending the digit b-1 to the left of the old value. With mathematical induction this will applies to any values of n
For example:
-256 in base 10:
3 digits: 744
4 digits: 9744
5 digits: 99744
-256 in base 7:
3 digits: 153
4 digits: 6153
5 digits: 66153

Related

Two's Complement Addition Resulting In A Negative

I understand the concept of using two's complement to represent a negative value which can then be used in addition to find the resulting value.
ex.
x = 15 and y = 10, in binary x = 1111 and y = 1010
for calculating x - y, we represent y as a negative using twos complement which results y = 0101 + 0001 = 0110 then carrying out the addition x + (-y) = 1111 + 0110 = 0101 which translates to the expected result 5.
All is well and fine until I try to use the same method to calculate y - x which ends up resulting in 1011 translating to 11, not the expected result -5. Is there a way to properly calculate the addition of two binary numbers resulting in a negative value?
Think of it this way; y - x is equivalent to -x + y. For that reason, we can first negate x, which is 15 (1111 in binary), which becomes 0001 after negation. We can then add that value to y (which is 10, 1010 in binary), which results in 1011. Because this value is negative, we then flip the bits and add 1 to make it positive, and we get 0101, which is 5 in base-10. This means that our result was -5, which is correct.
I think you were correct everywhere except where you stated that it translates to 11.

Given N bits, how many integers can be represented in binary?

Suppose you have 14 bits. How do you determine how many integers can be represented in binary from those 14 bits?
Is it simply just 2^n? So 2^14 = 16384?
Please note this part of the question: "how many INTEGERS can be represented in BINARY...". That's where my confusion lies in what otherwise seems like a fairly straightforward question. If the question was just asking how many different values or numbers can be represented from 14 bits, than yes, I'm certain it's just 2^n.
The answer depends on whether you need signed or unsigned integers.
If you need unsigned integers then using 2^n you can represent integers from 0 to 2^n exclusive. e.g. n=2; 2^2=4 you can represent the integers from 0 to 4 exclusive (0 to 3 inclusive). Therefore with n bits, you can represent a maximum unsigned integer value of 2^n - 1, but a total count of 2^n different integers including 0.
If you need signed integers, then half of the values are negative and half of the values are positive and 1 bit is used to indicate whether the integer is positive or negative. You then calculate using using 2^n/2. e.g. n=2; 2^2/2=2 you can represent the integers from -2 to 2 exclusive (-2 to +1 inclusive). 0 is considered postive, so you get 2 negative values (-2, -1) and 2 positive values (0 and +1). Therefore with n bits, you can represent signed integers values between (-) 2^n/2 and (+) 2^n/n - 1, but you still have a total count of 2^n different integers as you did with the unsigned integers.
Yes, it's that easy as 2^n.
A bit can have 2 distinct values: 0 and 1.
If you have 2 bits, than you have 4 distinct values: 00, 01, 10, 11. The list goes on.
Combinatorics has the simple counting formula
N = n_1 ⋅ n_2 ⋅ ... ⋅ n_k
Since n_1 = n_2 = n_k = 2 you can reduce the formula to
N = 2 ^ k

Binary digits in decimal

How do you represent (decimal) integer 50 in binary?
How many bits must be "flipped" in order to capitalize a lowercase 'a' that is represented in ASC11?
How do you represent the (decimal) integer 50 in, oh, "hexadecimal," otherwise known as base-16? Recall that decimal is simply base-10, and binary is simply base-2. Infer from those base systems how to represent this one?
Please answer these questions for me.HELP.
To help you some:
Binary is only made up of 1's and 0's.This may help you understand binary conversion
Decimal is 0-9
Hexadecimal is 0-9, then A-F (so A would represent 10, B would be 11, etc up to F which is 15)
Converting from decimal to another base
Here some tips for you regarding conversion to binary:
What is 50 mod 2? What about 25 mod 2 and then 12 mod 2? What are your results if you continue this?
What does any number mod 2 (always) return as result? - 1 or 0
Do you realise any patterns? - You get the reversed binary number as result
Test case 50:
50 mod 2 = 0 - 6th digit
25 mod 2 = 1 - 5th digit
12 mod 2 = 0 - 4th digit
6 mod 2 = 0 - 3rd digit
3 mod 2 = 1 - 2nd digit
1 mod 2 = 1 - 1st digit
The remainders of the divisions concatenated and reverses are: 110010, which is 50 in binary.
Can this be also transformed to further bases? - Yes, as we see with trying to convert 50 to hexadecimal:
50 mod 16 = 2 - 2nd digit
3 mod 16 = 3 - 1st digit
The remainders again concatenated and reversed are 32, which conveniently is 50 in hexadecimal.
In general we can say to convert a number to an arbitrary base you must take the remainder of the number and the base and then divide the number by the base and do the same thing again. In a program this would look something like:
while the number is greater 0 do:
result = (number mod base) + result;
number = number div base;
Converting from any base to decimal
How do you convert a number from an arbitrary base into base 10? First let us do a test case with binary. Lets take the 50 from the previous example: 110010
The method to convert from binary is multiplying every digit with the base to the power of the position of it in the number and adding up the result. The enumeration of the positions begins with 0 at the least significant digit. Our previous number would then look something like this:
1 *2^5 + 1 *2^4 + 0 *2^3 + 0 *2^2 + 1 *2^1 + 0 *2^0
What simplifies to:
32 + 16 + 2 = 50
It also works with any other base, like our 32 from the previous example:
3 *16^1 + 2*16^0 = 48 + 2 = 50
In program this would look something like this:
from end of number to beginning do:
result = result + digit * (base ^ position)

Calculating the total number of possibilities in binary?

How would you calculate the total number of possibilities that binary can have in one byte?
00000000 through 11111111 = num_of_possibilities
The total number is 2 to the power of the number of bits. So, eight bits has 28 possible values.
If you really mean "how to compute it", consider that each bit has two possible values.
So one bit implies 2 values.
Two bits has one set of two values of each possible value of the other bit, so
00
01
10
11
which means a total of 4 (= 2×2) values.
Three bits gives four values twice, or 8 (=4×2) values. Four bits, 8×2; five bits, 16×2, and so on.
So eight bits is 2×2×2×2×2×2×2×2 or 256.
It is a simple question: The number of possibilities is 2n where n is the number of bits.
So for 1 byte, which is 8 bits, there are 28 possibilites, 256.
There are several methods:
2^n where n is the number of bits (2^8) Each bit has 2 possibilities.
Unsigned value of all 1's + 1 (255 + 1) Count up from 0 to max value (all ones) + zero.
Build a tree where each leaf is the sum of the values to right and left of the new value from the row above. Possibilities is sum of row having n+1 entries. (2 ( 1 + 8 + 28 + 56 ) + 70) Each value is probability of that number of bits from 0 to n.

What is the "biggest" negative number on a 4-bit machine?

Or, what is the range of numbers that can be represented on a 4-bit machine using 2s-complement?
That would be -8 to +7
The range is -8 to 7, or 1000 to 0111. You can see the full range here.
4 bits (using 2's complement) will give you a range from -8 to 7.
This should be straightforward to work out yourself.
Range in twos complement will be:
-1 * 2 ^ (bits - 1)
to
2 ^ (bits - 1) - 1
So for 4 bits:
-1 * 2 ^ (4 - 1) = -1 * 2 ^ 3 = -8
to
2 ^ (4 - 1) - 1 = 2 ^ 3 - 1 = 7
Also, if you are interested and for others maybe browsing this question -
twos complement is used for easy binary arithmetic:
to add - you just add the two numbers without conversion and disregard the overflow:
-6 + 7 = 1
is
1010 = -6
0111 = 7
------
(1)0001 = 1 (ignoring the overflow)
...and more yet - to convert a negative binary number to its opposite positive number:
if the sign bit (highest order bit) is 1, indicating negative... reading from least significant to most significant bit (right to left), preserve every bit up through the first "1", then invert every bit after that.
So, with 8 bit
10011000 .. becomes
01101000 (* -1) = 104 * -1 = -104
and that is why 10000000 is your lowest negative number (or in X bit 1000.all zeroes..000), it translates to unsigned 10000000 * -1 = -128
Maybe a long winded answer but to those without the 1s and 0s background I figure it is useful
Well let's dissect this question.
Firstly, the question should be framed as - "The least*(because it is negative, so biggest is not the right usage)* possible negative number to be stored in 4-bit would be?"
Numbers are of two types -
Signed (holds both + and - numbers )
Unsigned (holds only + numbers)
We will be using binary representation to understand this.
For Unsigned -
4-bit minimum number = 0000 (0)
4-bit maximum number = 1111 (255)
So range would be Range : 0-15
For Signed 4-bits, first bit represent sign(+/-) and rest 3-bits represent number.
4-bit minimum will be a negative number.
So replace the first bit(MSB) with 1 in 0000(least unsigned number), making it 1000.
Calculate decimal equivalent of 1000 = 1*2^3 = 8
So, number would be -8 (- as we have 1 as the first bit in 1000)
4-bit maximum will be a positive number.
So replace the first bit(MSB) with 0 in 1111(largest unsigned number), making it 0111.
Calculate decimal equivalent of 0111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
So, number would be +7 (+ as we have 0 as the first bit in 0111)
Range would be -8 to +7.