Docsity

Data types Worksheet 5.docx, Exercises of Computer science

Toggle sidebar
bg1
Worksheet 5 Bitwise manipulation
Data types
Worksheet 5 Bitwise manipulation and masks
Task 1
1. Figure 1 shows a byte containing a signed integer. The value of the carry bit is unknown.
Figure 1
Show the result of performing the following shifts, starting each time with the byte given in
Figure 1.
(a) a logical right shift:
(b) a logical left shift:
(b) an arithmetic left shift:
(c) an arithmetic right shift:
2. Using a combination of shifts and addition, multiply 13 by 6
3. Figure 2 shows an 8-bit byte containing an integer.
Figure 2
Show the result of performing the following consecutive shifts on the byte.
(a) a circular right shift:
(b) a circular right shift:
(c) an arithmetic left shift:
(d) an arithmetic right shift:
1
carry
bit
carry
bit
01011001
01110001 0
pf2

Partial preview of the text

Download Data types Worksheet 5.docx and more Exercises Computer science in PDF only on Docsity!

Worksheet 5 Bitwise manipulation

Data types

Worksheet 5 Bitwise manipulation and masks

Task 1

  1. Figure 1 shows a byte containing a signed integer. The value of the carry bit is unknown. Figure 1 Show the result of performing the following shifts, starting each time with the byte given in Figure 1. (a) a logical right shift: (b) a logical left shift: (b) an arithmetic left shift: (c) an arithmetic right shift:
  2. Using a combination of shifts and addition, multiply 13 by 6
  3. Figure 2 shows an 8-bit byte containing an integer. Figure 2 Show the result of performing the following consecutive shifts on the byte. (a) a circular right shift: (b) a circular right shift: (c) an arithmetic left shift: (d) an arithmetic right shift: 1 carry bit carry bit

Worksheet 5 Bitwise manipulation

Data types

Task 2

  1. A system has 8 switches which are controlled by a binary code.Switches 1, 4 and 8 are currently ON. (a) It is desired to set switch 7 to a โ€˜1โ€™ without altering the other switches. Show how this can be done with a mask and a logical operator. Switch number 1 2 3 4 5 6 7 8 Current state 1 0 0 1 0 0 0 1 (b) It is now desired to set bits 1 to 4 to 0 without altering bits 5 to 8. Show how this can be done with a mask and a logical operator. Switch number 1 2 3 4 5 6 7 8 Current state 1 0 0 1 0 0 0 1
  2. The ASCII codes for the numbers 0 to 9 are from 0011 0000 to 0011 1001. Using the ASCII code for โ€œ1โ€ as an example, show how these can be translated into pure binary using an XOR mask and a logical operator. 1 2 3 4 5 6 7 8 code for 1 0 0 1 1 0 0 0 1 Show an alternative solution using a different mask. 1 2 3 4 5 6 7 8 code for 1 0 0 1 1 0 0 0 1 2