Question:

C programming: please help?

by  |  earlier

0 LIKES UnLike

i want to write a function that takes two parameters. a number A, and a value B. i want it to return 1 if the Bth bit from the right in the number A is 1, and return a 0 if it is 0.

example: A = 34 and B = 4, 34 in binary is 00100010, and the 4th bit from the right is a 0. So the function should return 0.

i need to do this using the masking to find a bit method.

thanks.

 Tags:

   Report

1 ANSWERS


  1. Here are two general ways to make the determination of a 0 or a 1.  You'll have to implement one or the other and fill in the details.

    1. You could use an array of values, each with one bit set -- the appropriate bit for that "value" -- then do a boolean mask with the correct one and examine the results, or,

    2. You could use the bitwise shift operator (>> or <<) to generate the appropriate mask.

    Hope that helps.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.