473,903 Members | 4,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
11 1660
14 New Member
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.
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
4,677 Recognized Expert Moderator Specialist
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

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 using %, /, and - operators,
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 fraction, a five bit value, 0-31. * */
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 think I let POST usages confuse me. .... D'oh! No, I hadn't thought of looking at the souce in my browser! Nothing like being half-armed and half-fast, huh? Guess I better switch
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 newbie. So, unable to take a call as to how-important or desirable the newer language features are -- so whether to write my app for v2.5 of Python, versus, as few others on this list have recommended, i.e. to stick to v2.3 ?? Are the preformance...
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 in c++/java !? Tnx, Raxit
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. Understand that XHTML is not for the general web. HTML is a better option. I own Eric Meyer's "CSS: The Definitive Guide" and am glad that I do. It is the best definitive guide I've read on any web topic. "Bullet Proof Web Design" is the best book...
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/
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.