简体   繁体   中英

Writing a function that returns 16-bit expression

The question following is excerpted from exercise 6, chapter 7, A book on C.

  1. Write a function that will extract every other bit position from a 32-bit expression. The result should be returned as a 16-bit expression. Your function should work on machines having either 2- or 4-byte words.

Now there are two key points I do not understand about this question:

  • What does it mean, "extract every other bit position"? I really do not understand this. For example, a 32-bit expression: 00000001 00000000 00000000 10011110, what can "extract every other bit position" do?
  • The question seems to contradict itself: "Extract every other bit position from a 32-bit expression," then "your function should work on machines having either 2- or 4-byte words," but 2-byte words are only 16-bit expression; how can I extract every other bit position from an expression that is "both" 32-bit and 16-bit?

This question really makes no senses. One of the things I really hate about this book is, compared to C prorgamming written by Kochan, which I have no problems at all in understanding its questions, so dense to understand with respect to exercise questions. Or may be I am just stupid!

"every other bit" means each either bits 0, 2, 4, 6 ... or bits 1, 3, 5, 7 ...

If you start with 32 bits and take each other bit then your result is 16 bits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM