473,703 Members | 2,420 Online

# bit fiddling question by a newbie

14 New Member
I need your guidance as to how to approach this question.
I don't even know where to begin. It seems a bit general for me. Thanks alot.

Devise a function to test bits in a byte, and it returns
a) success or failure,
b) count of bits that are set (“==”) in the byte,
c) position of bit(s) in the byte.
Jan 3 '08 #1
11 1638
sicarie
4,677 Recognized Expert Moderator Specialist
Devise a function to test bits in a byte, and it returns
a) success or failure,
Looks like your teacher is asking for a function prototype to me. That will settle what you are passing to it, and what you are returning from it. Then you can take b and c and use them to develop your algorithm for figuring out what needs to be the definition of the function (how you get what you are returning...)
Jan 3 '08 #2
new4cprog
14 New Member

Looks like your teacher is asking for a function prototype to me. That will settle what you are passing to it, and what you are returning from it. Then you can take b and c and use them to develop your algorithm for figuring out what needs to be the definition of the function (how you get what you are returning...)
Thanks sicarie. I'm kind of lost on this one. Could you or anyone else give me supporting example(s). I'm not visualizing it. I found it a little bit harder for me.

Thanks again.
Shulapi
Jan 3 '08 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Are you aware of functions - how they perform, what they do, how they are set up?
Jan 3 '08 #4
new4cprog
14 New Member
Are you aware of functions - how they perform, what they do, how they are set up?
sicarie - Sorry, for the similicity of the question.unders tanding the theortical part is a problem but implementing it the problem for me as a beginner. I know in general what function is and what it does: it need to be declared and defined. It has return type, name and arguments, it is called and returns to a caller. ex: int fun (in x, in y).
Jan 3 '08 #5
sicarie
4,677 Recognized Expert Moderator Specialist
No, that's great. So now we apply that to your specific issue. (I would like to point out that while you had an int as a return type, the types of your function parameters were declared as in - they need ints if you want them to hold integers.)

So back to the function - what datatype would be either a success or a failure?

Then, what are you passing to this function to be examined?
Jan 3 '08 #6
new4cprog
14 New Member
No, that's great. So now we apply that to your specific issue. (I would like to point out that while you had an int as a return type, the types of your function parameters were declared as in - they need ints if you want them to hold integers.)

So back to the function - what datatype would be either a success or a failure?

Then, what are you passing to this function to be examined?
I'm not sure but since it says byte, may be character datatype.
Jan 4 '08 #7
sicarie
4,677 Recognized Expert Moderator Specialist
Which was that for? I asked two questions there, which one do you think is a char datatype?
Jan 4 '08 #8
new4cprog
14 New Member
Which was that for? I asked two questions there, which one do you think is a char datatype?
I was thinking char for input since we are testing a bit in a byte and for success or failure that datatype will be int. Can you create one function for me to understand it better. Thanks
Jan 4 '08 #9
sicarie
4,677 Recognized Expert Moderator Specialist
I was thinking char for input since we are testing a bit in a byte and for success or failure that datatype will be int. Can you create one function for me to understand it better. Thanks
Well, I can see a char for the input - then you can compare it to the '1' or '0' char, though dealing in binary would be even easier.

However for the success or failure, I would recommend a boolean (bool) datatype. That return is true or false, while int is usually 0 for false and any other number for true, though it could work either way.
Jan 4 '08 #10