2 - Types, Operators, and Expressions
Exercise 2-1
Write a program to determine the ranges of
char,short,int, andlongvariables, bothsignedandunsigned, 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 optional0xor0X) into its equivalent integer value. The allowable digits are0through9,athroughf, andAthroughF. [1]
My solution
tbd
Official solution
tbd
Exercise 2-4
Write an alternate version of
squeeze(s1, s2)that deletes each character ins1that 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 strings1where any character from the strings2occurs, or-1ifs1contains no characters froms2. (The standard library functionstrpbrkdoes 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 returnsxwith thenbits that begin at positionpset to the rightmostnbits ofy, leaving the other bits unchanged. [1]
My solution
tbd
Official solution
tbd
Exercise 2-7
Write a function
invert(x, p, n)that returnsxwith thenbits that begin at positionpinverted (i.e.,1changed into0and 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 integerxrotated to the right bynbit 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