473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
11 1617
sicarie
4,677 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
sicarie
4,677 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
sicarie
4,677 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
sicarie
4,677 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
sicarie
4,677 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 <stdio.h>
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
sicarie
4,677 Expert Mod 4TB
Sicarie
what do you think of this -
#include <stdio.h>
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

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: saurabhnsit2001 | last post by:
I have got few questions on bit fiddling!! 1. Fast way of Multiplying a No by 7. 2. Counting no of set bits in a 32 bit no without looping. 3. finding id a no is divisible by 3 or not without...
22
by: dos.fishing | last post by:
Hello, I'm writing a function that should do the following: /** * Calculate and return fraction of valueA where max fractions is 31. * param valueA A five bit value, 0-31. * param valueB The...
0
by: Twayne | last post by:
Twayne wrote: That's the verification I was looking for I think. There are times that doesn't seem to be so, but perhaps I'll looking at the wrong side of the tree in the wrong forest ... I...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
10
by: Peter Michaux | last post by:
On May 14, 8:55 pm, Prisoner at War <prisoner_at_...@yahoo.comwrote: Get it from the library. I cannot imagine needing to own an HTML book. There are plenty of good references on the web....
2
by: r_ahimsa_m | last post by:
Could you recommend me some free JavaScript validator? I was using JSlint but it reports nonsense errors. Please help. Thanks. /RAM/
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.