Binary and Hexadecimal Calculator

Binary and Hexadecimal Calculator 23 2.19 You are given the following two numbers in two’s complement representation. Perform the binary addition. Did signed overflow occur? Explain how you determined whether or not overflow occurred. 01101110 +00011010 2.20 In this chapter the one’s complement number system was briefly mentioned, but at: http: courses.cs.vt.edu/csonline NumberSystems/Lessons/ you can learn about it. Explain the major trade-offs between using the one’s or two’s complement system 2.21 You are given the following two numbers in two’s complement representation Perform the binary subtraction. Did signed overflow occur? Explain how you determined whether or not overflow occurred. 11101000 – 00010011 2.22 Sign extend the 2 digit hex number 0x88 to a 4 digit hex number. Ox 2.23 The following subtract instruction is located at address 0x00012344. What are the two possible values for the contents of the Program Counter (PC) register after the branch instruction has executed? Ox__ ?? This branch instruction is described in Appendix C. loop: addi $t4, $t4,-8 sub $t2, $t2,$t0 bne $14, $t2, loop 2.24 You are given the following two 8-bit binary numbers in the two’s complement number system. What values do they represent in decimal? X = 100101002 = 10 Y = 001011002 =

The Correct Answer and Explanation is :

Let’s address each of your questions in detail:

2.19 Binary Addition and Overflow Detection

Given the two 8-bit two’s complement numbers:

01101110 (110)  // First number
+ 00011010 (26)  // Second number
--------------
  10001000 (-120)  // Sum

Overflow Detection:

In two’s complement arithmetic, overflow occurs when the result of an addition exceeds the representable range of the number system. For 8-bit two’s complement numbers, the range is from -128 to 127. Overflow is detected by examining the carry into and out of the most significant bit (MSB). If both carry bits are the same (both 0 or both 1), no overflow has occurred. If they differ, overflow has occurred.

In this case, adding 110 and 26 results in -120, which is within the representable range. Therefore, no signed overflow occurred.

2.20 Trade-offs Between One’s and Two’s Complement Systems

The one’s complement and two’s complement are two methods for representing signed binary numbers, each with its own advantages and disadvantages:

  • One’s Complement:
  • Advantages:
    • Simple to implement; negation involves flipping all bits.
  • Disadvantages:
    • Has two representations for zero: positive zero (all bits 0) and negative zero (all bits 1), which can complicate arithmetic operations.
    • Addition requires an end-around carry, adding complexity to hardware design.
  • Two’s Complement:
  • Advantages:
    • Only one representation for zero, simplifying arithmetic operations.
    • Addition and subtraction can be performed using the same hardware as for unsigned numbers, streamlining processor design.
  • Disadvantages:
    • Negation is slightly more complex: invert all bits and add one.

Overall, two’s complement is preferred in modern computing due to its simplicity in arithmetic operations and unambiguous representation of zero. (Stack Overflow)

2.21 Binary Subtraction and Overflow Detection

Given the two 8-bit two’s complement numbers:

11101000 (-24)  // First number
- 00010011 (19)  // Second number
--------------
  11010101 (-43)  // Difference

Overflow Detection:

In two’s complement subtraction, overflow occurs if the result exceeds the representable range. Here, subtracting 19 from -24 results in -43, which is within the range of -128 to 127. Therefore, no signed overflow occurred.

2.22 Sign Extension of Hexadecimal Number

To sign-extend the 2-digit hexadecimal number 0x88 to a 4-digit hexadecimal number:

  1. Convert 0x88 to binary:
  • 0x88 = 10001000
  1. Determine the sign bit:
  • The most significant bit (MSB) is 1, indicating a negative number in two’s complement.
  1. Extend the sign:
  • Copy the MSB to the new bits: 11111000
  1. Convert back to hexadecimal:
  • 11111000 = 0xF8

Therefore, the 4-digit hexadecimal representation is 0xF8.

2.23 Program Counter (PC) Values After Branch Instruction

The given assembly code is:

loop:
  addi $t4, $t4, -8
  sub $t2, $t2, $t0
  bne $14, $t2, loop

The bne (branch if not equal) instruction checks if registers $14 and $t2 are not equal. If they are not equal, it branches to the label loop.

The addi and sub instructions are executed sequentially before the bne.

The bne instruction is located at address 0x00012344.

If the branch is taken, the PC is updated to the target address, which is the address of the loop label.

If the branch is not taken, the PC is incremented by 4 to the next instruction.

Therefore, the two possible values for the PC after the bne instruction are:

  • If branch is taken: PC = address of loop
  • If branch is not taken: PC = 0x00012348

2.24 Decimal Values of Two 8-Bit Binary Numbers

Given the two 8-bit binary numbers:

  • X = 10010100
  • Y = 00101100

X = 10010100:

  1. Identify the sign bit:
  • The MSB is 1, indicating a negative number in two’s complement.
  1. Find the magnitude:
  • Invert all bits: 01101011
  • Add 1: 01101100 (0x6C)
  • 0x6C = 108 in decimal
  1. Apply the sign:
  • -108

Y = 00101100:

  1. Identify the sign bit:
  • The MSB is 0, indicating a positive number.
  1. Convert to decimal:
  • 0x2C = 44 in decimal

Therefore, X represents -108 and Y represents 44 in decimal.

For a visual explanation of two’s complement addition and overflow detection,

Scroll to Top