Skip navigation

Tag Archives: hexadecimal

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.23:

Consider the following C functions:

int fun1(unsigned word) {
  return (int) ((word << 24);
}

int fun2(unsigned word) {
  return ((int) word << 24;
}

Assume these are executed on a machine with a 32-bit word size that uses two's
complement aritmetic. Assume also that right shifts of signed values are
performed arithmetically, while right shifts of unsigned values are performed
logically.

A. Fill in the following table showing the effect of these functions for several
example arguments. You will find it more convienient to work with a
hexidecimal representation. Just remember that hex digits 8 through F have their
most significant bits equal to 1.

     w             fun1(w)       fun2(w)
-----------------------------------------------
0x00000076
-----------------------------------------------
0x87654321
-----------------------------------------------
0x000000C9
-----------------------------------------------
0xEDCBA987
-----------------------------------------------

B. Describe in words the useful computation each of these functions performs.

Answers

A.

word = 0x00000076

fun2(w)

(int) ((word << 24)

word << 24)

Still unsigned so performing logical right shift (adding 0's).

return value is 0x00000076

fun1(w)

((int) word << 24;

((int) word << 24)

current value is now a signed 0x76000000

Most signficant bit for this value is a 0.

Return value after right shift:

0x00000076

word = 0x87654321

fun1()

value is 0x21000000 after first operation
value is unsigned
returned value is 0x00000021

fun2()
value is 0x21000000 after first operation
value is signed
first signficant bit of 2 is 0
returned value is 0x00000021

word = 0x000000C9

fun1()
value is 0xC9000000 after first operation
value is unsigned
return value is 0x000000C9

fun2()
value is 0xC9000000 after first operation
value is signed
first signficant bit of C is 1
return value is 0xFFFFFFC9

word = 0xEDCBA987

fun1()
value is 0x87000000 after first operation
returned value is 0x00000087

fun2()
value is 0x87000000 after first operation
returned value is 0xFFFFFF87

     w             fun1(w)       fun2(w)
-----------------------------------------------
0x00000076       0x00000076     0x00000076
       118              118            118
-----------------------------------------------
0x87654321       0x00000021     0x00000021
2271560481               33             33
-----------------------------------------------
0x000000C9       0x000000C9     0xFFFFFFC9
       201              201            -55
-----------------------------------------------
0xEDCBA987       0x00000087     0xFFFFFF87
3989547399             135            -121
-----------------------------------------------

B)
fun1 reduces a number to its lower 8 bits and produces a value of 0-255
fun2 reduces a number the same but produces a value of -128-127

Advertisements

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.17: – this one was a lot of work :( – still fun to get it right though :P

For the lines labeled A-K in the following listing, convert the
hexadecimal values (two's complement) shown to the right of the instruction names
(not shown here, check your book, too much too type!) into their
decimal equivalents.

A. 0x184
B. 0x8
C. 0xc
D. 0x10
E. 0xfffffe94
F. 0x10
G. 0xfffffea0
H. 0xffffff10
I. 0x1c
J. 0xffffff7c
K. 0x18

A. 0x184

     1    8    4
  0001 1000 0100
  1098 7654 3210

     2^8 + 2^7 + 2^2 = 388

B. 0x8

   0x8 is 0x08 (hex binary values must have two digits to get 8 bits)

     0     8
  0000  1000 = 8


C. 0xc

   0xc = 0x0c (hex binary values must have two digits to get 8 bits)

     0    c
  0000 1100 =
       3210
       2^3 + 2^2 = 12

D. 0x10

     1    0
  0001 0000 = 2^4 = 16

E. 0xfffffe94

     f    f    f    f    f    e    9    4
  1111 1111 1111 1111 1111 1110 1001 0100
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^7 + 2^4 + 2^3 = -364


F. 0x10

     1    0
  0001 0000 = 16

G. 0xfffffea0

     f    f    f    f    f    e    a    0
  1111 1111 1111 1111 1111 1110 1010 0000
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^7 + 2^5 = -352

H. 0xffffff10

     f    f    f    f    f    f    1    0
  1111 1111 1111 1111 1111 1111 0001 0000
          0         0 5432 1098 7654 3210
   -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^8 + 2^4 = -240

I. 0x1c

     1    c
  0001 1100
     16 + 8 + 4 = 28

J. 0xffffff7c

     f    f    f    f    f    f    7    c
  1111 1111 1111 1111 1111 1111 0111 1100
          0         0 5432 1098 7654 3210
  -2^15 + 2^14 + 2^13 + 2^12 +  2^11 + 2^10 + 2^9 + 2^8 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 = -132

K. 0x18

     1    8
  0001 1000
     16 + 8 = 24

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.15:

Fill in the table below showing the effects of the different shift operations
on single-byte quantities. The best way to think about shift oeprations
is to work with binary representations. Convert the initial values to binary, 
perform shifts, and then convert back to hexadecimal. Each of the answers 
should be 8 binary digits or 2 hexadecimal digits.



-------------------------------------------------------------------------
       x                x << 2           x >> 2
                                        (Logical)       (Arithmetic)
-------------------------------------------------------------------------
  Hex     Binary    Hex     Binary    Hex     Binary    Hex     Binary
-------------------------------------------------------------------------
  0xF0
-------------------------------------------------------------------------
  0x0F
-------------------------------------------------------------------------
  0xCC
-------------------------------------------------------------------------
  0x55
-------------------------------------------------------------------------


0xF0

  0xF0
  11110000

  x << 2 (Logical)

  0x3C
  00111100

  x >> 2 (Arithmetic)

  0xFC
  11111100

0x0F

  0x0F
  00001111

  x << 2 (Logical)

  0x03
  00000011

  x >> 2 (Arithmetic)

  0x03
  00000011


0xCC

  0xCC
  11001100

  x << 2 (Logical)

  0x33
  00110011

  x >> 2 (Arithmetic)

  0xF3
  11110011

0x55

  0x55
  01010101

  x << 2 (Logical)

  0x15
  00010101

  x >> 2 (Arithmetic)

  0x15
  00010101

-------------------------------------------------------------------------
       x                x << 2           x >> 2
                                        (Logical)       (Arithmetic)
-------------------------------------------------------------------------
  Hex     Binary    Hex     Binary    Hex     Binary    Hex     Binary
-------------------------------------------------------------------------
  0xF0   11110000  0x80    10000000  0x3C    00111100  0xFC   11111100
-------------------------------------------------------------------------
  0x0F   00001111  0x78    01111000  0x03    00000011  0x03   00000011
-------------------------------------------------------------------------
  0xCC   11001100  0x60    01100000  0x33    00110011  0xF3   11110011
-------------------------------------------------------------------------
  0x55   01010101  0xA8    10101000  0x15    00010101  0x15   00010101
-------------------------------------------------------------------------


This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.7:

Code for show bytes.

What would be printed as a result of the following call to show_bytes?

char *s = "ABCDEF";
show_bytes( s, strlen( s ) );


Note that letters 'A' through 'Z" have ASCII codes 0x41 through 0x5A

41 42 43 44 45 46


This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.4:

Without converting the numbers to decimal or binary, try to solve the following
arithmetic problems, giving the answers in hexadecimal. Hint: just modify
the mothods you use for performing decimal addition and subtraction to use
base 16.

A. 0x502c + 0x8 =
B. 0x502c - 0x30 =
C. 0x502c + 64 =
D. 0x50da - 0x502c =

A. 0x502c + 0x8 =

  0x502c
  +  0x8
  ------
  0x5034

B. 0x502c - 0x30 =

  0x502c
  - 0x30
  ------
  0x4ffc


C. 0x502c + 64 =

  64 = 4 * 16 + 0
  4 = 0 * 16 + 4
  64 = 0x40

  0x502c
  + 0x40
  ------
  0x506c


D. 0x50da - 0x502c =

  0x50da
 -0x502c
 -------
    0xae

A. 0x502c + 0x8 = 0x5034
B. 0x502c - 0x30 = 0x4ffc
C. 0x502c + 64 = 0x506c
D. 0x50da - 0x502c = 0xae


This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.3:

A single byte can be represented by two hexadecimal digits. Fill in the missing 
entries in the following table, giving the decimal, binary and hexadecimal 
values of different byte patterns.

-------------------------------
Decimal   Binary    Hexadecimal
-------------------------------
    0    00000000       00
-------------------------------
   55
-------------------------------
  136
-------------------------------
  243
-------------------------------
         01010010
-------------------------------
         10101100
-------------------------------
         11100111
-------------------------------
                        A7
-------------------------------
                        3E
-------------------------------
                        BC
-------------------------------


1)
Decimal = 55
  55/16 = 3
  16 * 3 = 48
  56 - 48 = 7
 A) 55 = 3 * 16 + 7 (7)
  3/16 = 0
 B) 3 = 0 * 16 + 3 (D)
 Hexadecimal = 0x37
 Binary = 00010111

2)
Decimal = 136
    8 * 16 = 128
    136 - 128 = 8
  A) 136 = 8 * 16 + 8
    8/16 = 0
  B) 8 = 0 * 16 + 8
  Hexadecimal = 0x88
  Binary = 10001000


3)
Decimal = 243
    16 * 15 = 240
    243 - 240 = 3
  A) 243 = 15 * 16 + 3
  B) 15 = 0 * 16 + 15
  Hexadecimal = 0xF3
  Binary = 11110011

4)
Binary = 01010010
  Hexadecimal = 0x52
    = 5 * 16 + 2
    = 50 + 30 + 2
  Decimal = 82

5)
Binary = 10101100
  Hexadecimal = 0xAC
      = 10 * 16 + 12
      = 160 + 12
  Decimal = 172

6)
Binary = 11100111
  Hexadecimal = 0xE7
      = 14 * 16 + 7
      = 224 + 7
  Decimal = 231

7)
Hexadecimal = A7
  Binary = 10100111
      = 128+32+4+2+1
  Decimal = 167

8)
Hexadecimal = 3E
  Binary = 00111110
      = 2 + 4 + 8 + 16 + 32
  Decimal = 62

9)
Hexadecimal = BC
  Binary = 10111100
      = 4 + 8 + 16 + 32 + 128
  Decimal = 188

-------------------------------
Decimal    Binary    Hexadecimal
-------------------------------
    0    0000 0000       00
-------------------------------
   55    0001 0777       37
-------------------------------
  136    1000 1000       88
-------------------------------
  243    1111 0011       F3
-------------------------------
   82    0101 0010       52
-------------------------------
  172    1010 1100       AC
-------------------------------
  231    1110 0111       E7
-------------------------------
  167    1010 0111       A7
-------------------------------
   62    0011 1110       3E
-------------------------------
  188    1011 1100       BC
-------------------------------


This note is for the book I am currently reading. All of my notes and solutions are available at Google Code.

To convert decimal value x to hexadecimal, we repeatedly devide by 16
giving a quotent q and a reamainder r such that x = q * 16 + 4
r becomes the *least* signficant digit for the hexadecimal value

As an example: x = 314156

  314145 = 19634 * 16 + 12 (C)
   19634 = 1227 * 16 + 2   (2)
    1227 = 76 * 16 + 11    (B)
      76 = 4 * 16 + 12     (C)
       4 = 0 * 16 + 4      (4)

314156 in hexadecimal is 0x4CB2C

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.2:

Problem

Fill in the blank entries in the following table, given the decimal and
hexadecimal representation of different powers of 2:


n     2^n (Decimal)       2^n (Hexadecimal)
--------------------------------------------
11        2048                 0x800
--------------------------------------------
 7
--------------------------------------------
          8192
--------------------------------------------
                             0x20000
--------------------------------------------
16
--------------------------------------------
           256
--------------------------------------------
                                0x20
--------------------------------------------

1)
  n = 7
  2^7 = Decimal form: 128
  7 = i + 4j where 0 <= i <= 3
    7 = 3 + 4 ( 1 )
    i = 3, j = 1
    Hexadecimal form: 0x80

2)
  Decimal value = 8192
  8192 log2 = n = 13
  13 = i + 4j where 0 <= i <= 3
    13 = 1 + 4 ( 3 )
    13 = 1 + 12
    i = 1, j = 3
    Hexadecimal form: 0x2000
3)
  Hexadecimal value = 0x20000
    i = 1
    j = 4
    n = i + 4j
      n = 1 + 4 * 4
      n = 1 + 16
      n = 17
   Decimal value = 2^n = 131,072

4)
  n = 16
  Decimal value = 2^n = 65,536
  n = i + 4j where 0 <= i <= 3
    16 = i + 4j
    16 = 0 + 4*4
    i = 0, j = 4
    Hexadecimal: 0x10000

5)
  Decimal value = 256
  n = 8
    8 = i + 4j where 0 <= i <= 3
    i = 0, j = 2
    Hexadecimal value: 0x100

7)
  Hexadecimal value = 0x20
  i = 1, j= 1
  n = i + 4j
  n = 1 + 4*1
  n = 5
  Decimal value = 2^n = 2^5 = 32

n     2^n (Decimal)       2^n (Hexadecimal)
--------------------------------------------
11        2048                 0x800
--------------------------------------------
 7         128                  0x80
--------------------------------------------
13         8192               0x2000
--------------------------------------------
17       131072              0x20000
--------------------------------------------
16        65536              0x10000
--------------------------------------------
 8          256                0x100
--------------------------------------------
 5           32                 0x20
--------------------------------------------

This note is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Given that x in decimal form is a power of 2: x = 2 ^ n

Binary form of x is 1 followed by n 0's

Hexadecimal form of x is:
  write n in the form of i + 4j where 0 <= i <= 3
  i is the is a key to get the digit of the hexadecimal form, followed by j 0's
  to get the first digit of the hexadecimal form:
    if i = 0: first digit is 1
    if i = 1: first digit is 2
    if i = 2: first digit is 4
    if i = 3: first digit is 8

Example:

x = 2048
x = 2^11

n      = 11
i + 4j = 11 where 0 <= i <= 3
    4*2 = 8 + 3 = 11
i = 3, j = 2;
answer is: 0x800

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.1:

A. 0x8F7A93 to binary

     8    F    7    A    9    3
  1000 1111 0111 1010 1001 0011

  Answer: 100011110111101010010011

B. Binary 1011011110011100 to hexadecimal

  1011 0111 1001 1100
     B    7    9    C

  Answer: 0xB79C