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. With x = 75, what is the value of y after the following assignment?

8-bit: x ← 75, y
y ← (x << 4) ∨ (x >> 4)
  1. A75
  2. B180
  3. C210
Show answer and explanation

Answer: B. 180

75 is 0100 1011 in binary. x << 4 moves the low 4 bits to the top, giving 1011 0000 = 176 (the 0100 shifted out is discarded), and x >> 4 moves the high 4 bits to the bottom, giving 0000 0100 = 4. Their OR is 1011 0100 = 180: the high and low 4 bits are swapped. Treating x << 4 as 75 × 16 = 1200 without limiting it to 8 bits would change the result.

  • A. This assumes the swap leaves the value unchanged. The high and low 4 bits differ, so the value changes.
  • B. Correct. Swapping the halves of 0100 1011 gives 1011 0100 = 180.
  • C. This is 1101 0010, the 8 bits in reverse order.
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. 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?
  2. ÷ 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?
  3. 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?
  4. A queue uses the 4-element string array q as a ring. head and tail start at 1. enq(v) does q[tail] ← v and then tail ← tail mod 4 + 1; deq() returns the value of q[head] and then does head ← head mod 4 + 1. When the following program runs, in which element of q is "E" stored?
  5. The following program outputs a linked list, whose elements are linked by the array next, by following it from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What sequence of values is output?