22.1 Fill in the blanks in each of the following:
The bits in the result of an expression using the operator are set to one if the corresponding bits in each operand are set to one. Otherwise, the bits are set to zero.
The bits in the result of an expression using the operator are set to one if at least one of the corresponding bits in either operand is set to one. Otherwise, the bits are set to zero.
Keyword introduces a structure declaration.
Keyword is used to create a synonym for a previously defined data type.
Each bit in the result of an expression using the operator is set to one if exactly one of the corresponding bits in either operand is set to one.
The bitwise AND operator &
is often used to bits (i.e., to select certain bits from a bit string while zeroing others).
The and operators are used to shift the bits of a value to the left or to the right, respectively.
22.2 Write a single statement or a set of statements to accomplish each of the following:
Define a structure called Part
containing int
variable partNumber
and char
array partName
, whose values may be as long as 25 characters.
Define PartPtr
to be a synonym for the type Part*
.
Use separate statements to declare variable a
to be of type Part
, array b[10]
to be of type Part
and variable ptr
to be of type pointer to Part
.
Read a part number and a part name from the keyboard into the members of variable a
.
Assign the member values of variable a
to element three of array b
.
Assign the address of array b
to the pointer variable ptr
.
Print the member values of element three of array b
, using the variable ptr
and the structure pointer operator to refer to the members.
22.3 Write a single statement to accomplish each of the following. Assume that variables c
(which stores a character), x, y
and z
are of type int
; variables d, e
and f
are of type double
; variable ptr
is of type char*
and arrays s1[100]
and s2[100]
are of type char
.
Convert the character stored in c
to an uppercase letter. Assign the result to variable c
.
Determine if the value of variable c
is a digit. Use the conditional operator as shown in Figs. 22.18–22.20 to print " is a "
or " is not a "
when the result is displayed.
Convert the string "1234567"
to long
, and print the value.
Determine whether the value of variable c
is a control character. Use the conditional operator to print " is a "
or " is not a "
when the result is displayed.
Assign to ptr
the location of the last occurrence of c
in s1
.
Convert the string "8.63582"
to double
, and print the value.
Determine whether the value of c
is a letter. Use the conditional operator to print " is a "
or " is not a "
when the result is displayed.
Assign to ptr
the location of the first occurrence of s2
in s1
.
Determine whether the value of variable c
is a printing character. Use the conditional operator to print " is a "
or " is not a "
when the result is displayed.
Assign to ptr
the location of the first occurrence in s1
of any character from s2
.
Assign to ptr
the location of the first occurrence of c
in s1
.
Convert the string "-21"
to int
, and print the value.
bitwise AND (&
).
bitwise inclusive OR (|
).
struct
.
typedef
.
bitwise exclusive OR (^
).
mask.
left-shift operator (<<
), right-shift operator (>>
).
struct Part {
int partNumber;
char partName[26];
};
typedef Part* PartPtr;
Part a;
Part b[10];
Part* ptr;
cin >> a.partNumber >> a.partName;
b[3] = a;
ptr = b;
cout << (ptr + 3)->partNumber << ' '
<< (ptr + 3)->partName << endl;
c = toupper(c);
cout << ''' << c << "' "
<< (isdigit(c) ? "is a" : "is not a")
<< " digit" << endl;
cout << atol("1234567") << endl;
cout << ''' << c << "' "
<< (iscntrl(c) ? "is a" : "is not a")
<< " control character" << endl;
ptr = strrchr(s1, c);
out << atof("8.63582") << endl;
cout << ''' << c << "' "
<< (isalpha(c) ? "is a" : "is not a")
<< " letter" << endl;
ptr = strstr(s1, s2);
cout << ''' << c << "' "
<< (isprint(c) ? "is a" : "is not a")
<< " printing character" << endl;
ptr = strpbrk(s1, s2);
ptr = strchr(s1, c);
cout << atoi("-21") << endl;