|
|||||
| | |||||
Conversion to base 2 from base 10 means dividing and using the remainders as the coefficents. When performing this division, the whole number part is the part which is then used again to get a new remainder and so forth until at the bottom of the work a zero is all that remains. Use division if a whole number is being converted, or multiplication if a fraction is being converted. The fractional portion is again multiplied.
when converting from base 10 to base 2, the first division by 2 , the remainder from that division is the least-significant bit. For example, to convert 53 to base 2, divide 53 by 2 to give 26 with a remainder of 1. 1 is the least-significant bit. in other words, it is the right-most bit.
where least-significant bit means Bit zero, the bit of a binary number giving the number of ones, the last or rightmost bit when the number is written in the usual way. (source: wombat.doc.ic.ac.uk/foldoc/foldoc.cgi)
when converting from base 10 to base 2, the first division by 2 , the remainder from that division is the least-significant bit. For example, to convert 53 to base 2, divide 53 by 2 to give 26 with a remainder of 1. 1 is the least-significant bit.
in this way, 97 gives
2|97 R == 1
2|48 R == 0
2|24 R == 0
2|12 R == 0
2|6 R == 0
2|3 R == 1
2|1 R == 1
===> 1100001 base 2. THat occupies 7 bits.
in this way, 219 gives ===> 11011011 base 2. THat occupies 8 bits and doesn't fit in a char. it will fit in an unsigned char.
to try these conversions yourself please download the binary to decimal c conversion program from here(bindec.c).
For a sample program see wraparaound.c and its output wraparound_c_out.txt.
now, how does that 219 get printed as -37 when we attempt to print that char as a %d in the printf statement (in wraparound.c)? It comes out to be 100101 in base 2. It doesn't look anything like 219 in binary, either as a complement or anything. Where does the negative sign come from, then?
To convert say from base 4 to base 7 convert base 4 to base 10 then compute from base 10 to base 7
letters are used here to denote numbers > 9 because we only have 9 numbers. Note that because the system starts counting at zero, that A is not 11 but is nine, and, likewise, F is denoted by 15 not 16.
converting from 0xF2 to decimal ...
A2F base 16 = 10 * 16^2 + 2 * 16^1 + 15 * 10^0 = 2560 + 32 + 15 = 2607 in base 10.
do the base part as usual
if there is a fraction, need multiply that fraction repeatedly by 16. The whole number part of the product is the first digit to the right of the decimal point (in the new hex number). With the fractional part, repeat again the multiplication by 16 ... until there is only a whole number which remains. Sometimes, they stop that process early, however.
little-endian: Little-endian. A computer architecture that stores multiple-byte numerical values with the least significant byte (LSB) values first.
as explained in numerous google pages ..... willl not link to them, just use google
little_endian: Describes a computer architecture in which, within a given 16- or 32-bit word, bytes at lower addresses have lower significance (the word is stored ‘little-end-first’)
(microsoft page link)
little-endian refers to the byte order system not the bit ordering system. Do not needlessly confuse with least-significant bit. Byte order has instead low-order byte and high-order byte.
|
to start out, why is -37 represented instead of 219 when adding 'a' and 'z' as in wraparaound.c and its output wraparound_c_out.txt? Ignoring the first byte in 219, as it goes past 7, gives a base 2 binary number of 11011011. Please see that file for my difficulties in coercing 1011011 (dropping the first bit for the sign) and then converting to "37" decimal. For example, the first system used is sign and magnitude. 219 is represented as 11011011. Now, let's say the first bit is used for the negative sign, we come up with 1011011 (negative) -=< (backwards) 1+2+8+16+64=91.
The answer lies in the following:
to convert from a 7-bit postitive to its negation, drop the first bit from the number to make room for the negative sign. Then, take the 2's complement of the remaining 7 bits. That is the magnitude of your number.The above coincides with the decimal character representation in C of a character whose size is greater than 127, as in wraparound.c
The point is, we determine whether the number is stored as a negative or positive integer at the time we define our datatype (true atleast in C, i don't know about the other languages)
see /languages/c/ISO_c_basics/data_tipz.htm
this is the storage used most commonly to store negative numbers
http://www.wordiq.com/definition/2147483647_(number)
note that 1000 represents minus zero in the sign and magnitude system and -8 in the 2's complement system. Because the first bit is used as a place holder, the highest we can go in a 4-bit system is "-7" which in binary is 1111. Then, 1000 represents -0.
2's = 0 + magnitude;
For a negative number, type "negative numbers and binary subtraction" in my search engine. It appears from that page that binary 1000 comes up with -8 . However, as above, to convert to a negative number, take the 2's complement of the remaining bits after dropping the first bit for the sign. Also, binary 1001 (the next in sequence) comes up with -7 instead of -1. Remember, 2's complement is one's complement of the rest of the bits except for the last one. My book: "For an n-bit word, the first bit is the sign and the remaining n-1 bits represent the magnitude of the number. THIS IS THE DIFFERENCE BETWEEN SIGN-AND-MAGNITUDE AND 2'S COMPLEMENT. In fact, I was really talking about 2's complement above.
dont' forget that when taking the 2's complement fo anumber, sure, yeah complement all bits after the first '1' encountered leaving that first one alone, but, also, leave the first bit as a '1'.
N* denotes 2 complement
N* = 2^n - N, where 'n' is the length in bits. For that, if n=4, -N is represented by 16 -N, so -3 = 16 -3 = 13 = 1102 (base 2).
2's complement can be formed by complementing every bit and then adding one.
but, a faster way to make it, is to take the complement of every binary digit to the left of the first one (and leave that first digit alone)
N witha bar on top of it represents one's complement. N-bar = (2^n -1 ) -N
1111 represents zero
And to make one complement, just take the complement of every bit in the positive number. This is the same as subtracting a bit from one.
In all these numbering systems, when it is negative, it has a '1' in the most significant bit.
How do you find the magnitude of a number in one's and two's complement?
I would have to say take the complement for one's complement and the complement of all but the first one for two's complement (notes above on conversion from decimal to one/two's omplement)? ?
Two's complement addition follows the same rules as binary addition
the left-over digit to the left of the result is ignored.
source = http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm
add +5 and -6 in two's complement: 0101
we say that an overflow occurs if the correct representation of the sum (including sign) requires more than n bits when the word length is n bits. YOu have to look carefully -- the result is correct even if there is overflow if when you ignore the carry, the answer is correct. (that begs the question, but so what it's school)
The left-most digit is carried around and added to the least-significant bit of the answer.
does end-around carry,where the last leftover bit wraps around to the right side
1010 (-6)
----
1111 (-1 in 2's complement)
suprrisingly +6 + -5 is also correct:
1011
0110
----
(1)0001 -- correct, just ignore the overflow?
detect the error when the addition of two positive numbers yields a negative number.
and when the addition of two negatives yield a positive
in the above 2 cases detection is easy
| Leave a Reply |