home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16391 < prev    next >
Encoding:
Internet Message Format  |  1992-11-12  |  3.9 KB

  1. Xref: sparky comp.lang.c:16391 comp.edu:1896
  2. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!modus!gear!cadlab!martelli
  3. From: martelli@cadlab.sublink.org (Alex Martelli)
  4. Newsgroups: comp.lang.c,comp.edu
  5. Subject: Re: Help needed with C Bitwise Exercises.
  6. Message-ID: <1992Nov11.110507.27872@cadlab.sublink.org>
  7. Date: 11 Nov 92 11:05:07 GMT
  8. References: <1992Nov7.155205.24673@news.columbia.edu>
  9. Organization: CAD.LAB S.p.A., Bologna, Italia
  10. Lines: 76
  11.  
  12. hauben@cunixf.cc.columbia.edu (Michael Hauben) writes:
  13.     ...
  14. :    I have two problems for my C Programming Language class
  15. :that I am not quite sure how to approach. This C class has been
  16. :frustrating because much of the problems (less so these) have
  17. :been particularly obscure, and the actual class hasn't helped in
  18. :figuring the exercise out. The Problems are as follows:
  19.  
  20. Many consider it bad form to use the net for class assignments,
  21. but I believe in this case you're going about it the right way:
  22. i.e., being upfront about it and asking for approaches on how
  23. to THINK about the problems, rather than for somebody else to
  24. do them for you.  So...:
  25.  
  26. :1) Write a function that will reverse the bit representation of
  27. :an int. Here are two examples if a reversing operation defined
  28. :for a char instead of an int:
  29. :
  30. :    01110101 reversed yields  10101110
  31. :    10101111 reversed yields  11110101
  32. :
  33. :and 
  34. :
  35. :2) Write a function that will extract every other bit position
  36. :from a 32-bit expression. The result should be returned as a
  37. :16-bit expression. Your function should work on machines having
  38. :either 2- or 4-byte words.
  39. :
  40. :    I would greatly appreciate any help in how to approach
  41. :these problems.
  42.  
  43. *ABSTRACT* - that's the keyword.  If, instead of being about
  44. integers as bitstrings, the same problems were about "arrays
  45. of bits", wouldn't it be easier to see your way to a solution?
  46. I.e. 1) becomes "write an array the same as another but
  47. reversed", 2) becomes "write an array having every other element
  48. from another one twice as long" (say all those with even index
  49. 0 upwards).  I believe you can solve these; sketch a solution,
  50. BUT, do NOT use 'array[index]' to read the index-th element or
  51. 'array[index]=value' to write it; instead, ABSTRACT them into
  52. macros:
  53.  
  54. #define GETELEM(array,index)       (array)[index]
  55. #define PUTELEM(array,index,value) ((array)[index]=(value))
  56.  
  57. Write a solution using these macros, and a simple program to
  58. test your solution function.  Also keep the array lengths
  59. symbolic via #define - remember, you must ABSTRACT!  You
  60. cannot "return an array from a function" in C so for this
  61. stage you will have to build the array to be returned as a
  62. local static array of your solution-function, and return
  63. the address of this local static array (receiving it into
  64. a pointer) - a minor stumbling block though.
  65.  
  66. NOW - you ALMOST have your solution, except that now you know
  67. you aren't using arrays, but "bitstrings" which are really
  68. unsigned integers (possibly long or short ones, for the
  69. 16-bit and 32-bit specs).  So think back to these macros which
  70. extract or place an "element" into an "array".  Do you know
  71. how to extract one bit from an integer?  Hints: bitwise
  72. operations, shifting the constant "1" appropriately, testing
  73. against zero... do you know how to PLACE one bit in an
  74. integer?  Hints: set-to-1 is easy if you have the "extract"
  75. thingy above right; set-to-0's hints are "&" and "~"...
  76.  
  77. You should now have enough hints to implement and test.
  78. The solution you obtain this way will NOT be optimized.  Must
  79. you optimize?  In this case, think about whether there is a
  80. better overall way to extract all bits from an integer, one
  81. after another in a given order, and, similarly, to set all
  82. bits in an integer, possibly in a different order.  Hints:
  83. the shift operator is your friend, and it comes in two
  84. directions...
  85. -- 
  86. Email: martelli@cadlab.sublink.org                   Phone: ++39 (51) 6130360
  87. CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: ++39 (51) 6130294 
  88.