Confused about this code regarding bits

This is a discussion on Confused about this code regarding bits within the C Programming forums, part of the General Programming Boards category; Part of an exercise in my book requests "Write a function called bit_test that takes two arguments: an unsigned int ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Confused about this code regarding bits

    Part of an exercise in my book requests "Write a function called bit_test that takes two arguments: an unsigned int anda bit number n. Have the function return 1 bit number n if it is on inside the
    word, and 0 if it is off."

    The if statement of the function that deals with this is:
    if ( (word >> (31 - n)) & 0x1 )
    return 1;


    I don't understand what it is saying.
    The argument shifts word to the right by (31-n) which is AND'ed with the hexidecimal 1. What does that mean?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    The argument shifts word to the right by (31-n) which is AND'ed with the hexidecimal 1. What does that mean?
    I could answer that but since you were able to type all of that I wonder about your real question. Do you know what any of that means or does every operation need to be explained? I'd rather start by building on what you know.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Oh. I actually didn't understand the question "Have the function return 1 bit number n if it is on inside the
    word, and 0 if it is off." That is pasted verbatim, but is there a typo? I don't understand what it's saying.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    OK so binary numbers, like all numbers, have a place value. 2^31, 2^30, 2^29, ... 2^0, and in each place value there is either a 0 where the place value is not included, or a 1, which it is, and that sums up the value of the number. What you were asked to do is test place value n if it is 1 or 0. The code you posted solved the problem. Did you solve the problem? Do you understand the solution at all? That is what I've asked you from the start.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    if ( (word >> (31 - n)) & 0x1 )
    So if the argument inside the if statement is 1, then 1 is returned.
    The word is shifted over 31-n places to the right. If the value anded with 1 results in 1, it is on and returns 1. Is that correct?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help with this code, I am confused
    By rjh9136 in forum C++ Programming
    Replies: 6
    Last Post: 11-20-2011, 10:38 PM
  2. Confused with following code
    By SeriousTyro in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 10:37 AM
  3. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 09:39 PM
  4. I'm confused as to what exactly is going on in this code
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2002, 01:22 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21