Programming Basics (Pseudocode Tracing)

x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. What is the value of y after the following program runs?

8-bit: x ← 13, y
y ← (x << 2) ∨ (x >> 1)
  1. A27
  2. B54
  3. C58
Show answer and explanation

Answer: B. 54

13 is 00001101 in binary. x << 2 shifts it 2 bits left to 00110100 = 52, and x >> 1 shifts it 1 bit right to 00000110 = 6 (the lowest 1 shifted out is discarded). Their OR is 00110110 = 54. 52 and 6 both have the bit for 4 set, so the OR differs from the sum 52 + 6 = 58.

  • A. This shifts in the opposite directions: (x >> 2) ∨ (x << 1) = 3 ∨ 26.
  • B. Correct. 00110100 ∨ 00000110 = 00110110 = 54.
  • C. This adds instead of ORing. They share a 1 bit (the 4s bit), so the results differ.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. ∧ is bitwise AND. What is the value of c after the following program runs?
  2. x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. With x = 75, what is the value of y after the following assignment?
  3. The following procedure bubble is a bubble sort that sorts the integer array data in ascending order. swap(data, j, k) is a procedure that swaps the values of data[j] and data[k]. Array indices start at 1. What should replace 【a】 in the program?
  4. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. The following program finds in lo the index of the first value that is at least x in the integer array data, which is sorted in ascending order (indices start at 1). x is no greater than the last element of data. What should replace 【a】 in the program?
  5. A stack is represented by the 5-element integer array st and the integer variable top. All elements of st start at 0 and top starts at 0. push(v) adds 1 to top and then does st[top] ← v; pop() returns the value of st[top] and then subtracts 1 from top (the elements of st are not changed). What are top and st after the following program runs?