
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
النسخة 2الرقم المعياري الدولي: 978-0131409095
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
النسخة 2الرقم المعياري الدولي: 978-0131409095 تمرين 1
Write and test a function printBinary() that displays int values in binary using the number of bits used in your version of C++ to store ints. ( Note : sizeof ( T ) gives the amount of memory allocated for values of type T. ) For example, if ints are allocated 32 bits, the function should display 00000000000000000000000000010001 for the value 17.
One way to produce this output is with the help of C++ bitwise operators as outlined in the following algorithm (if these are not familiar to you, you can find information about them in Appendix C and on the text's website):
1. Initialize an unsigned int variable mask , with a value whose binary representation has a 1 followed by all 0s; e.g., 10000000000000000000000000000000 for 32-bit representation. ( Helpful Hint : 0x80000000 is the hexadecimal representation of an integer constant with this bit pattern.)
2. For a counter running from 1 through the number of bits, do the following:
a. If the bitwise-and ( ) of the given integer and mask is nonzero
?Display'1' Else Display '0'
b. Shift the bits in mask one position to the right (using ),
Suggestion : Work through an example by hand to ensure that you understand how the algorithm works before trying to convert it to code.
One way to produce this output is with the help of C++ bitwise operators as outlined in the following algorithm (if these are not familiar to you, you can find information about them in Appendix C and on the text's website):
1. Initialize an unsigned int variable mask , with a value whose binary representation has a 1 followed by all 0s; e.g., 10000000000000000000000000000000 for 32-bit representation. ( Helpful Hint : 0x80000000 is the hexadecimal representation of an integer constant with this bit pattern.)
2. For a counter running from 1 through the number of bits, do the following:
a. If the bitwise-and ( ) of the given integer and mask is nonzero
?Display'1' Else Display '0'
b. Shift the bits in mask one position to the right (using ),
Suggestion : Work through an example by hand to ensure that you understand how the algorithm works before trying to convert it to code.
التوضيح
Program Plan:
Main method:
• Declare t...
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
لماذا لم يعجبك هذا التمرين؟
أخرى 8 أحرف كحد أدنى و 255 حرفاً كحد أقصى
حرف 255