2 - Types, Operators, and Expressions
Exercise 2-1
Write a program to determine the ranges of
char
,short
,int
, andlong
variables, bothsigned
andunsigned
, by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types. [1]
My solution
tbd
Official solution
tbd
Exercise 2-2
Write a loop equivalent to the for loop above without using
&&
or!!
. [1]
My solution
tbd
Official solution
tbd
Exercise 2-3
Write the function
htoi(s)
, which converts a string of hexadecimal digits (including an optional0x
or0X
) into its equivalent integer value. The allowable digits are0
through9
,a
throughf
, andA
throughF
. [1]
My solution
tbd
Official solution
tbd
Exercise 2-4
Write an alternate version of
squeeze(s1, s2)
that deletes each character ins1
that matches any character in the strings2
. [1]
My solution
tbd
Official solution
tbd
Exercise 2-5
Write the function
any(s1, s2)
, which returns the first location in the strings1
where any character from the strings2
occurs, or-1
ifs1
contains no characters froms2
. (The standard library functionstrpbrk
does the same job but returns a pointer to the location.) [1]
My solution
tbd
Official solution
tbd
Exercise 2-6
Write a function
setbits(x, p, n, y)
that returnsx
with then
bits that begin at positionp
set to the rightmostn
bits ofy
, leaving the other bits unchanged. [1]
My solution
tbd
Official solution
tbd
Exercise 2-7
Write a function
invert(x, p, n)
that returnsx
with then
bits that begin at positionp
inverted (i.e.,1
changed into0
and vice versa), leaving the others unchanged. [1]
My solution
tbd
Official solution
tbd
Exercise 2-8
Write a function
rightrot(x, n)
that returns the value of the integerx
rotated to the right byn
bit positions. [1]
My solution
tbd
Official solution
tbd
Exercise 2-9
In a two's complement number system,
x &= (x-1)
deletes the rightmost 1-bit inx
. Explain why. Use this observation to write a faster version ofbitcount
. [1]
My solution
tbd
Official solution
tbd
Exercise 2-10
Rewrite the function
lower
, which converts upper case letters to lower case, with a conditional expression instead ofif-else
. [1]
My solution
tbd
Official solution
tbd