467,081 Members | 1,227 Online

# bit fiddling question by a newbie

 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
• viewed: 1279
Share:
11 Replies
 Expert Mod 4TB Okay, well, I would start with this: 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
 Okay, well, I would start with this: 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
 Expert Mod 4TB Are you aware of functions - how they perform, what they do, how they are set up? Jan 3 '08 #4
 Are you aware of functions - how they perform, what they do, how they are set up? sicarie - Sorry, for the similicity of the question.understanding 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
 Expert Mod 4TB 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
 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
 Expert Mod 4TB Which was that for? I asked two questions there, which one do you think is a char datatype? Jan 4 '08 #8
 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
 Expert Mod 4TB 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
 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. Sicarie what do you think of this - #include int main() { unsigned int sucess, bitinfo[9] = {0}; success = function( bitinfo, sizeof(bitinfo) ); . . . int function( unsigned int *bitinfo , unsigned int size_of_bitinfo) Thanks a lot for you help - shulapi Jan 6 '08 #11
 Expert Mod 4TB Sicarie what do you think of this - #include int main() { unsigned int sucess, bitinfo[9] = {0}; success = function( bitinfo, sizeof(bitinfo) ); . . . int function( unsigned int *bitinfo , unsigned int size_of_bitinfo) Thanks a lot for you help - shulapi new4cprog - That looks like you're definitely starting to get it, though you're still using an int for your function() return type - I would recommend a bool as it it s definite success or failure/true or false. You will also need the function prototype pulled outside the main() function, and the definition is usually below the end of the main(). You are definitely headed in the right direction, but I was curious as to why you're not using a char like you said. I agree that an int* is easier - then you can examine the binary data itself using the binary operators, and then also the size_of_bitinfo - if you're only looking at a byte then you know the length... So you are getting it set out, are you familiar with boolean operations like these? Jan 6 '08 #12