What are the steps to convert the signed binary in two's (2's) complement representation to an integer in decimal system (in base ten)?
1. Is this a positive or a negative number?
1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 1000 is the binary representation of a negative integer, on 64 bits (8 Bytes).
- In a signed binary in two's complement representation, the first bit (the leftmost) indicates the sign, 1 = negative, 0 = positive.
2. Get the binary representation in one's complement.
* Run this step only if the number is negative
- Note on binary subtraction rules:
- 11 - 1 = 10; 10 - 1 = 01; 1 - 0 = 1; 1 - 1 = 0.
Subtract 1 from the initial binary number.
1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 1000 - 1 = 1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 0111
3. Get the binary representation of the positive (unsigned) number.
* Run this step only if the number is negative
Flip all the bits of the signed binary in one's complement representation (reverse the digits) - replace the bits set on 1 with 0s and the bits on 0 with 1s:
!(1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 0111) = 0111 1000 1010 1011 1111 0000 1000 0101 0000 0000 0000 0000 0000 0000 0101 1000
4. Map the unsigned binary number's digits versus the corresponding powers of 2 that their place value represent:
263
0 262
1 261
1 260
1 259
1 258
0 257
0 256
0 255
1 254
0 253
1 252
0 251
1 250
0 249
1 248
1 247
1 246
1 245
1 244
1 243
0 242
0 241
0 240
0 239
1 238
0 237
0 236
0 235
0 234
1 233
0 232
1 231
0 230
0 229
0 228
0 227
0 226
0 225
0 224
0 223
0 222
0 221
0 220
0 219
0 218
0 217
0 216
0 215
0 214
0 213
0 212
0 211
0 210
0 29
0 28
0 27
0 26
1 25
0 24
1 23
1 22
0 21
0 20
0
5. Multiply each bit by its corresponding power of 2 and add all the terms up.
0111 1000 1010 1011 1111 0000 1000 0101 0000 0000 0000 0000 0000 0000 0101 1000(2) =
(0 × 263 + 1 × 262 + 1 × 261 + 1 × 260 + 1 × 259 + 0 × 258 + 0 × 257 + 0 × 256 + 1 × 255 + 0 × 254 + 1 × 253 + 0 × 252 + 1 × 251 + 0 × 250 + 1 × 249 + 1 × 248 + 1 × 247 + 1 × 246 + 1 × 245 + 1 × 244 + 0 × 243 + 0 × 242 + 0 × 241 + 0 × 240 + 1 × 239 + 0 × 238 + 0 × 237 + 0 × 236 + 0 × 235 + 1 × 234 + 0 × 233 + 1 × 232 + 0 × 231 + 0 × 230 + 0 × 229 + 0 × 228 + 0 × 227 + 0 × 226 + 0 × 225 + 0 × 224 + 0 × 223 + 0 × 222 + 0 × 221 + 0 × 220 + 0 × 219 + 0 × 218 + 0 × 217 + 0 × 216 + 0 × 215 + 0 × 214 + 0 × 213 + 0 × 212 + 0 × 211 + 0 × 210 + 0 × 29 + 0 × 28 + 0 × 27 + 1 × 26 + 0 × 25 + 1 × 24 + 1 × 23 + 0 × 22 + 0 × 21 + 0 × 20)(10) =
(0 + 4 611 686 018 427 387 904 + 2 305 843 009 213 693 952 + 1 152 921 504 606 846 976 + 576 460 752 303 423 488 + 0 + 0 + 0 + 36 028 797 018 963 968 + 0 + 9 007 199 254 740 992 + 0 + 2 251 799 813 685 248 + 0 + 562 949 953 421 312 + 281 474 976 710 656 + 140 737 488 355 328 + 70 368 744 177 664 + 35 184 372 088 832 + 17 592 186 044 416 + 0 + 0 + 0 + 0 + 549 755 813 888 + 0 + 0 + 0 + 0 + 17 179 869 184 + 0 + 4 294 967 296 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 64 + 0 + 16 + 8 + 0 + 0 + 0)(10) =
(4 611 686 018 427 387 904 + 2 305 843 009 213 693 952 + 1 152 921 504 606 846 976 + 576 460 752 303 423 488 + 36 028 797 018 963 968 + 9 007 199 254 740 992 + 2 251 799 813 685 248 + 562 949 953 421 312 + 281 474 976 710 656 + 140 737 488 355 328 + 70 368 744 177 664 + 35 184 372 088 832 + 17 592 186 044 416 + 549 755 813 888 + 17 179 869 184 + 4 294 967 296 + 64 + 16 + 8)(10) =
8 695 307 959 590 191 192(10)
6. If needed, adjust the sign of the integer number by the first digit (leftmost) of the signed binary:
1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 1000(2) = -8 695 307 959 590 191 192(10)
The number 1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 1000(2), signed binary in two's (2's) complement representation, converted and written as an integer in decimal system (base ten):
1000 0111 0101 0100 0000 1111 0111 1010 1111 1111 1111 1111 1111 1111 1010 1000(2) = -8 695 307 959 590 191 192(10)
Spaces were used to group digits: for binary, by 4, for decimal, by 3.