hw2-Elgamal-Muhab

docx

School

New Jersey Institute Of Technology *

*We aren’t endorsed by this school

Course

CS350

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

7

Uploaded by JudgeOkapi3713

Report
CS350 Intro Computer Systems Homework 2 Homework 2 Representing and Manipulating Numbers 1. Convert the following hexa decimals to binary, unsigned integers, and integers, following the format of the practice problem 2.17 of textbook. This is not a programming assignment although I recomend you to do it in C. Hexa decimal Binary B2U8(x) B2T8(x) 0x0A 1010 10 -6 0x06 110 6 -2 0x14 1 0100 20 -12 0x6B 110 1011 107 -21 0x8A 1000 1010 138 -118 0x86 1000 0110 134 -122 0x94 1001 0100 148 -108 0xEB 1110 1011 235 -21 2. Write a C program to compute and output exactly as shown in Figure 2.14 of textbook. Use the variables and values defined in /usr/include/limits.h. Do not hardcode them, meaning do not write these numbers directly in the program. Derive from the variables and values defined in the header file. 3. #include <stdio.h> 4. #include <limits.h> 5. 6. // Define a line separator 7. #define LINE "-------------------------------------------------------------------------------------- \n " 8. 9. // Function to print a line separator 10. void print_line () { 11. printf ( " %s " , LINE ); 12. } 13. 14. int main () { 15. // Print a line separator 16. print_line (); 17. 18. // Print a header for the table 19. printf ( " %40s \n " , "Word Size w" ); 20. 21. // Print a line separator 22. print_line (); 23.
24. // Print column headers 25. printf ( " %-8s %-18s %-18s %-18s %-18s \n " , "Value" , "8" , "16" , "32" , "64" ); 26. 27. // Print a line separator 28. print_line (); 29. 30. // Print unsigned max values for different word sizes 31. printf ( " %-8s 0x %-15X 0x %-15X 0x %-15X 0x %-15llX \n " , "UMaxw" , UCHAR_MAX , USHRT_MAX , UINT_MAX , ULLONG_MAX ); 32. printf ( " %-8s %-17hhu %-17hu %-17u %-17llu \n\n " , "UMaxw" , UCHAR_MAX , USHRT_MAX , UINT_MAX , ULLONG_MAX ); 33. 34. // Print minimum and maximum values for different word sizes 35. printf ( " %-8s 0x %-15X 0x %-15X 0x %-15X 0x %-15llX \n " , "TMinw" , CHAR_MIN , SHRT_MIN , INT_MIN , LLONG_MIN ); 36. printf ( " %-8s %-17hhd %-17hd %-17d %-17lld \n\n " , "TMaxw" , CHAR_MIN , SHRT_MIN , INT_MIN , LLONG_MIN ); 37. 38. printf ( " %-8s 0x %-15X 0x %-15X 0x %-15X 0x %-15llX \n " , "TMinw" , CHAR_MAX , SHRT_MAX , INT_MAX , LLONG_MAX ); 39. printf ( " %-8s %-17hhd %-17hd %-17d %-17lld \n\n " , "TMaxw" , CHAR_MAX , SHRT_MAX , INT_MAX , LLONG_MAX ); 40. 41. // Print -1 in different formats 42. printf ( " %-8s 0x %-15hhX 0x %-15hX 0x %-15X 0x %-15llX \n\n " , "-1" , ( char ) - 1 , ( short int ) - 1 , ( int ) - 1 , ( long long int ) - 1 ); 43. 44. // Print 0 in different formats 45. printf ( " %-8s 0x %02hhX \t\t 0x %04hX \t\t 0x %08X \t\t 0x %016llX \n " , "0" , ( char ) 0 , ( short int ) 0 , ( int ) 0 , ( long long int ) 0 ); 46. 47. // Print a line separator 48. print_line (); 49. 50. return 0 ; 51. } 52.
3.) For mapping between signed and unsinged using bits, refer to pages 23 and 24 of the lecture notes labeled 2nd and 3rd. Write the mapping for w=5. Follow the format in the lecture note. This is not a programming assignment although I recommend you to do it in C. 53. #include <stdio.h> 54. 55. int main () { 56. // Declaration of variables 57. char sep = '%' ; 58. int sign , unsign ; 59. 60. // Prompt the user to enter a signed value 61. printf ( "Enter signed value:" ); 62. 63. // Read the signed value from the user 64. scanf ( " %d " , & sign ); 65. 66. // Determine the unsigned value based on the input signed value 67. if ( sign >= 0 && sign <= 7 ) 68. unsign = sign ; 69. else if ( sign >= - 8 && sign <= - 1 ) 70. unsign = 16 + sign ; 71. 72. // Print the unsigned value with the separator character 73. printf ( "Unsigned : %d%c " , unsign , sep ); 74. 75. return 0 ; // Indicate successful program execution 76. } 77.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4.) Write a C program to demonstrate casting surprises of page 28 of lecture note labeled 2nd and 3rd. #include <stdio.h> int main () { const int TMIN = - 2147483648 ; // Minimum value of a 32-bit signed integer const int TMAX = 2147483647 ; // Maximum value of a 32-bit signed integer unsigned int constant1 = 0 ; // Unsigned zero int constant2 = - 1 ; // Signed negative one unsigned int u_value ; // User-provided unsigned integer int s_value ; // User-provided signed integer printf ( "Enter an unsigned value: " ); scanf ( " %u " , & u_value ); printf ( "Enter a signed value: " ); scanf ( " %d " , & s_value ); printf ( "constant1 %s constant2 \n " , ( constant1 == constant2 ) ? "==" : "!=" ); printf ( "constant1 %s constant2 \n " , ( constant1 < constant2 ) ? "<" : ">=" ); printf ( "u_value %s constant1 \n " , ( u_value == constant1 ) ? "==" : "!=" ); printf ( "constant2 %s s_value \n " , ( constant2 < s_value ) ? "<" : ">=" ); printf ( "constant1 %s u_value \n " , ( constant1 > u_value ) ? ">" : "<=" ); printf ( "constant2 %s (unsigned int)constant1 \n " , ( constant2 > ( unsigned int ) constant1 ) ? ">" : "<=" ); printf ( "(int)constant1 %s constant2 \n " , (( int ) constant1 > constant2 ) ? ">" : "<=" ); return 0 ;
} 5.) For signed and unsigned addition, fill in the following table in the style of Practice problem 2.29 of textbook for w=5: Type x y x+y x+(t5)y Case integer 13 5 18 -14 3 binary 01101 00101 010010 10010 integer 3 4 7 7 3 binary 00011 00100 00 0111 0 0111 integer -8 7 -1 -1 2 binary 11000 00111 11111 11111 integer -9 -7 -16 -16 1 binary 10111 11001 11 0000 1 0000 integer -11 -14 -25 7 4 binary 10101 10010 10 0111 0 0111 6.) Do problem 2.73: write a C function saturating_add. 7.) #include <stdio.h> 8.) 9.) /* Predefined values for TMAX and TMIN */ 10.) #define TMax 2147483647 11.) #define TMin ( - TMax - 1 ) 12.) 13.) /* FUNCTION to saturating_add(int, int) */ 14.) int saturating_add ( int firstNumber , int secondNumber ) 15.) { 16.)
17.) // Calculate the total number of bits for the given data type (e.g., int) 18.) int bitCount = sizeof ( firstNumber ) << 3 ; // 4 bytes * 8 bits = 32 bits 19.) 20.) // Calculate the sum of firstNumber and secondNumber 21.) int sum = firstNumber + secondNumber ; 22.) 23.) // Create a mask with the most significant bit set 24.) int mask = 1 << ( bitCount - 1 ); // For a 32-bit int, this sets the leftmost bit 25.) 26.) // Extract the most significant bit of firstNumber 27.) int mostSigFirstNumber = firstNumber & mask ; 28.) 29.) // Extract the most significant bit of secondNumber 30.) int mostSigSecondNumber = secondNumber & mask ; 31.) 32.) // Extract the most significant bit of the sum 33.) int mostSigSum = sum & mask ; 34.) 35.) // Check for positive overflow (if both numbers are non-negative but the sum is negative) 36.) int positiveOF = ~ mostSigFirstNumber & ~ mostSigSecondNumber & mostSigSum ; 37.) 38.) // Check for negative overflow (if both numbers are negative but the sum is non-negative) 39.) int negativeOF = mostSigFirstNumber & mostSigSecondNumber & ! mostSigSum ; 40.) 41.) // Adjust the sum to TMax if positive overflow occurs 42.) if ( positiveOF ) { 43.) sum = TMax ; 44.) } 45.) // Adjust the sum to TMin if negative overflow occurs 46.) else { 47.) sum = TMin ; 48.) } 49.) return sum ; // Return the saturated sum 50.) } 51.) 52.) /* MAIN FUNCTION STARTS HERE */ 53.) int main (){ 54.) // TEST CASE 55.) int sum = saturating_add ( TMin , - 5 ); 56.) 57.) // DISPLAY THE RESULT OF THE TEST CASE 58.) printf ( "The Sum Is : %d \n\n " , sum ); 59.) } 60.)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help