I'm having trouble converting the following two’s complement binary number to decimal
01110000
Step 1: Invert the bits -> 10001111
Step 2: Add 1 to the bit -> 10010000
Therefore, the decimal value is 144
However, I used online converter and it says the decimal value is 112
The value of ...111 0111 0000 (padded with an infinite number of 1's) is -144. The value of ...000 0111 0000 (padded with an infinite number of 0's) is 112. Given the former, one could compute its additive inverse by inverting all the bits (yielding ...000 1000 1111) and adding 1 (yielding ...000 1001 0000, i.e. 144).
Hexadecimal in mips: If I have 0x80000000 and I subtract it by 0xD00000000
my answer is 0X -50000000
is this possible in mips having a negative in or is there another way to write this? That is correct?
Remember, if you look at the most significant bit, you'll know if you are working with a negative number. For simplicity, let's just look at the four most significant bits.
0x80 = 1000 0000
0xD0 = 1101 0000
So those values are already negative. You want to subtract them, that is, 0x80 - 0xD0. Well, subtraction is addition (a-b = a + -b), and the whole point of 2's compliment is that you can add signed numbers and get the result you expect. So let's negate 0xD0:
1101 0000 # original value
0010 1111 # flip the bits
0011 0000 # add 1
If that didn't make sense, ask the all-knowing Wikipedia.
Now, we can add the values:
1000 0000
0011 0000
---------
1011 0000
So if I've done it right, 0x80 - 0xD0 = 0xB0. It's still a negative number (but you don't put a minus sign in front of it, it's implied by the MSBit). And that makes sense, because 0x80 is a very negative number, and 0xD0 is a less negative number than 0x80. (Really, it is... 0xFF is -1, the least negative number.)
Can anyone tell me what is lower and higher bits?. How to identify a higher and lower bit?. Below is binary form. How does 0110 is higher bit in it?.
0110 0111 1100 1010 1100 0111 1001 1011
Just like in decimal, higher places are generally written to the left in binary. So if you see 0111, the 0 is the high bit. So this would represent 7 in decimal. The same applies when spaces are used, just like when commas (or dots, depending on your locale) are used to separate groups of digits in decimal.
From the book, Art of Assembly, I copy this quote:
In the two’s complement system, the H.O. bit of a number is a sign bit. If the H.O. bit is zero, the number is positive; if the H.O. bit is one, the number is negative. Examples:
For 16-bit numbers:
8000h is negative because the H.O. bit is one.
100h is positive because the H.O. bit is zero.
7FFFh is positive.
0FFFFh is negative.
0FFFh is positive.
I don't understand the last two examples. If you convert the two examples to binary, you get 0000 1111 1111 1111 1111 for the first and 0000 1111 1111 1111 for the second. Why is the former negative and the latter positive? It seems to me that the highest order bit for both would be 0 and therefor both should be positive.
The reason for the leading 0 on 0FFFFH is to give the
assember/compiler a hint that F is part of a number. Not all
assemblers require this.
So the negative number is in reality FFFFh, so 1111 1111 1111 1111, then is negative.
computer-programming-forum.com/46-asm/1b99282efbac3bcf.htm
The text says: 16-bit numbers. So you need to look at the 16th bit from the right. In 0FFFF, that would be a 1. As for the leading zero, it's notational hint that the value is a number, not a word (i. e. not a variable).
Parsers (including assemblers) have easier time parsing numeric literals if you establish a convention that a valid number can only start with a digit. So do some humans. DEADBEEF is a valid hex number, y'know.
could you explain why 0FFFF has 5 digits? Is it the same as FFFF
It is not the same. Just plain FFFFh will be interpreted as a symbol by the assembler. And you'll get a compile error since it cannot find any symbol named "FFFFh". Putting a 0 in front of it ensures that the assembler will interpret it as a number.
if the number should be 16 bits, the 16th bit is taken as the sign bit. in the first,
0FFFFh
the 16th bit is 1 as it is
0000 1111 1111 1111 1111
in the second example,
0FFFh
the 16th bit is 0 as it is
0000 1111 1111 1111
the 16th bit is 0, though there are more than 16 digits, binary considers only the first 16 digits. so, the first is negative and the second is positive
Out of curiosity, how exactly does binary code get converted into letters? I know there are sites that automatically convert binary to words for you but I wanna understand the specific, intermediary steps that binary code goes through before being converted into letters.
Here's a way to convert binary numbers to ASCII characters that is often simple enough to do in your head.
1 - Convert every 4 binary digits into one hex digit.
Here's a binary to hex conversion chart:
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = a (the hex number a, not the letter a)
1011 = b
1100 = c
1101 = d
1110 = e
1111 = f
(The hexadecimal numbers a through f are the decimal numbers 10 through 15. That's what hexadecimal, or "base 16" is - instead of each digit being capable of representing 10 different numbers [0 - 9], like decimal or "base 10" does, each digit is instead capable of representing 16 different numbers [0 - f].)
Once you know that chart, converting any string of binary digits into a string of hex digits is simple.
For example,
01000100 = 0100 0100 = 44 hex
1010001001110011 = 1010 0010 0111 0011 = a273 hex
Simple enough, right? It is a simple matter to convert a binary number of any length into its hexadecimal equivalent.
(This works because hexadecimal is base 16 and binary is base 2 and 16 is the 4th power of 2, so it takes 4 binary digits to make 1 hex digit. 10, on the other hand, is not a power of 2, so we can't convert binary to decimal nearly as easily.)
2 - Split the string of hex digits into pairs.
When converting a number into ASCII, every 2 hex digits is a character. So break the hex string into sets of 2 digits.
You would split a hex number like 7340298b392 this into 6 pairs, like this:
7340298b392 = 07 34 02 98 b3 92
(Notice I prepended a 0, since I had an odd number of hex digits.)
That's 6 pairs of hex digits, so its going to be 6 letters. (Except I know right away that 98, b3 and 92 aren't letters. I'll explain why in a minute.)
3 - Convert each pair of hex digits into a decimal number.
Do this by multiplying the (decimal equivalent of the) left digit by 16, and adding the 2nd.
For example, b3 hex = 11*16 + 3, which is 110 + 66 + 3, which is 179.
(b hex is 11 decimal.)
4 - Convert the decimal numbers into ASCII characters.
Now, to get the ASCII letters for the decimal numbers, simply keep in mind that in ASCII, 65 is an uppercase 'A', and 97 is a lowercase 'a'.
So what letter is 68?
68 is the 4th letter of the alphabet in uppercase, right?
65 = A, 66 = B, 67 = C, 68 = D.
So 68 is 'D'.
You take the decimal number, subtract 64 for uppercase letters if the number is less than 97, or 96 for lowercase letters if the number is 97 or larger, and that's the number of the letter of the alphabet associated with that set of 2 hex digits.
Alternatively, if you're not afraid of a little bit of easy hex arithmetic, you can skip step 3, and just go straight from hex to ASCII, by remembering, for example, that
hex 41 = 'A'
hex 61 = 'a'
So subtract 40 hex for uppercase letters or 60 hex for lowercase letters, and convert what's left to decimal to get the alphabet letter number.
For example
01101100 = 6c, 6c - 60 = c = 12 decimal = 'l'
01010010 = 52, 52 - 40 = 12 hex = 18 decimal = 'R'
(When doing this, it's helpful to remember that 'm' (or 'M') is the 13 letter of the alphabet. So you can count up or down from 13 to find a letter that's nearer to the middle than to either end.)
I saw this on a shirt once, and was able to read it in my head:
01000100
01000001
01000100
I did it like this:
01000100 = 0100 0100 = 44 hex, - 40 hex = ucase letter 4 = D
01000001 = 0100 0001 = 41 hex, - 40 hex = ucase letter 1 = A
01000100 = 0100 0100 = 44 hex, - 40 hex = ucase letter 4 = D
The shirt said "DAD", which I thought was kinda cool, since it was being purchased by a pregnant woman. Her husband must be a geek like me.
How did I know right away that 92, b3, and 98 were not letters?
Because the ASCII code for a lowercase 'z' is 96 + 26 = 122, which in hex is 7a. 7a is the largest hex number for a letter. Anything larger than 7a is not a letter.
So that's how you can do it as a human.
How do computer programs do it?
For each set of 8 binary digits, convert it to a number, and look it up in an ASCII table.
(That's one pretty obvious and straight forward way. A typical programmer could probably think of 10 or 15 other ways in the space of a few minutes. The details depend on the computer language environment.)
Assuming that by "binary code" you mean just plain old data (sequences of bits, or bytes), and that by "letters" you mean characters, the answer is in two steps. But first, some background.
A character is just a named symbol, like "LATIN CAPITAL LETTER A" or "GREEK SMALL LETTER PI" or "BLACK CHESS KNIGHT". Do not confuse a character (abstract symbol) with a glyph (a picture of a character).
A character set is a particular set of characters, each of which is associated with a special number, called its codepoint. To see the codepoint mappings in the Unicode character set, see http://www.unicode.org/Public/UNIDATA/UnicodeData.txt.
Okay now here are the two steps:
The data, if it is textual, must be accompanied somehow by a character encoding, something like UTF-8, Latin-1, US-ASCII, etc. Each character encoding scheme specifies in great detail how byte sequences are interpreted as codepoints (and conversely how codepoints are encoded as byte sequences).
Once the byte sequences are interpreted as codepoints, you have your characters, because each character has a specific codepoint.
A couple notes:
In some encodings, certain byte sequences correspond to no codepoints at all, so you can have character decoding errors.
In some character sets, there are codepoints that are unused, that is, they correspond to no character at all.
In other words, not every byte sequence means something as text.
Do you mean the conversion 011001100110111101101111 → foo, for example? You just take the binary stream, split it into separate bytes (01100110, 01101111, 01101111) and look up the ASCII character that corresponds to given number. For example, 01100110 is 102 in decimal and the ASCII character with code 102 is f:
$ perl -E 'say 0b01100110'
102
$ perl -E 'say chr(102)'
f
(See what the chr function does.) You can generalize this algorithm and have a different number of bits per character and different encodings, the point remains the same.
To read binary ASCII characters with great speed using only your head:
Letters start with leading bits 01. Bit 3 is on (1) for lower case, off (0) for capitals. Scan the following bits 4–8 for the first that is on, and select the starting letter from the same index in this string: “PHDBA” (think P.H.D., Bachelors in Arts). E.g. 1xxxx = P, 01xxx = H, etc. Then convert the remaining bits to an integer value (e.g. 010 = 2), and count that many letters up from your starting letter. E.g. 01001010 => H+2 = J.
http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp it just looks here... (not HERE but it has a table).
There are 8 bits in a byte. One byte can be one symbol. One bit is either on or off.
Why not just do this take 010010001001001 split it into two bits 8 letter each (01001000, 01001001). Then issue the powers
01001000. 01001001.
The first 8 ignore the first three they determine if it's capital or not, the go right to left doing powers of 2 (2^1, 2^2 2^3 2^4 2^5). So then add all the ones up , there's only one, and it = 8, and te eight letter in the alphabet is h so our first bit is the letter h, try it on the other bit