Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
November 18th, 2012, 12:38 PM
 C505
Registered User

Join Date: Nov 2012
Posts: 23
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0
Bit_test function

I hope this forum doesnt mind me asking for help on homework assignments. If it is a bother, please let me know. So far the help I have gotten here is better than any help session at school.

5. Write a function called bit_test that takes two arguments: an unsigned int and a bit number n. Have the function return 1 bit number n if it is on inside the word, and 0 if it is off. Assume that bit number 0 references the leftmost bit inside the integer. Also write a corresponding function called bit_set that takes two arguments: an unsigned int and a bit number n. Have the function return the result of turning bit n on inside the integer.

I have a skeleton of the code so far. Anyone see any problems with it as written?

Code:
```#include <stdio.h>

int main(void)
{
unsigned int input;
unsigned int compare;
unsigned int bit_test(unsigned int compare, int input);

printf("\n\nEnter the number to compare to:\n");
scanf("%u",&compare);
printf("\n\nEnter a number to test:\n");
scanf("%i",&input);
bit_test(compare, input);
}

unsigned int bit_test(unsigned int testvalue, int n)
{
}

unsigned int bit_set(unsigned int setvalue, int n)
{
}```

#2
November 18th, 2012, 12:49 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Your skeleton is not really about what is asked.
You are asked to check if a specific bit of a number is set/clear and to set/clear a specific bit of a number

The bit #0 of the number 1012 is clear; the bit #7 of the number 65535 is set.

If you set bit #0 of the number 4, you obtain number 5 back; if you clear bit #1 of number 0 (bit #1 is already cleared) you get number 0 back.

#3
November 18th, 2012, 01:04 PM
 C505
Registered User

Join Date: Nov 2012
Posts: 23
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0
Quote:
 Originally Posted by bdb Your skeleton is not really about what is asked. You are asked to check if a specific bit of a number is set/clear and to set/clear a specific bit of a number The bit #0 of the number 1012 is clear; the bit #7 of the number 65535 is set. If you set bit #0 of the number 4, you obtain number 5 back; if you clear bit #1 of number 0 (bit #1 is already cleared) you get number 0 back.

I hadnt even thought about the method to write the two functions yet. I find that if I think functions first I screw up syntax later. I still have problems prototyping and calling functions. I see what you are saying, im not sure how to implement it tho. I was reading a little about bit fields, thats the only way I see to write to a specific bit.

#4
November 18th, 2012, 01:17 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
To write the functions prototypes don't worry about implementation details: what matters is what you send in to the function and what you get back.

First function: test a specific bit.
Inputs: an unsigned value; a bit number
Outputs: yes or no (true or false; 0 or 1)
prototype: int bitstatus(unsigned value, int bitnumber);
Example: if (bitstatus(1012, 0)) /* ... */;

Second function: set/clear a specific bit (let's make it 2 functions).
Inputs: an unsigned value; a bit number
Outputs: an unsigned value
prototype1: unsigned bitset(unsigned value, int bitnumber);
prototype2: unsigned bitclear(unsigned value, int bitnumber);
Example: newvalue = bitclear(0, 0);

When you get to write code, you can use this reasoning as comments to explain what the function does :-)

#5
November 18th, 2012, 01:32 PM
 C505
Registered User

Join Date: Nov 2012
Posts: 23
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0

Thanks, that is helpful, I like 2 functions for the second part.

I was thinking for bit_test:

if check & test = 1 // boolean &
then return true // test is inside check
else shift check // shift the bits in check (>>)
then test check & test again.

but how to exit the loop... maybe a for loop?

#6
November 19th, 2012, 08:45 AM
 C505
Registered User

Join Date: Nov 2012
Posts: 23
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0
Quote:
 Originally Posted by Lux Perpetua See also: http://graphics.stanford.edu/~seander/bithacks.html("counting bits set") for some other neat ways to do this.

Thanks, this is a great site. I found some code on here I would like to use for another problem, but I dont understand how it works. I want to try to use the following pieces of code to finish this problem.

Code:
```#include <stdio.h>

#define haszero(v) (((v) - 0x01010101UL) & ~(v) & 0x80808080UL)
#define hasvalue(x,n) \ (haszero((x) ^ (~0UL/255 * (n))))```

We havent covered any #define statements in my class. Are haszero and hasvalue functions? If so what do they return and how would I call them?

#7
November 19th, 2012, 10:31 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
They aren't functions, technically, though they behave somewhat like functions from the programmer's point of view. They are what are called macros, and they are called just like functions are, but the compiler (or rather, the pre-processor) treats them differently.

To understand this, you need to know that C actually consist of two languages: the pre-processor language, which as the name suggests is processed before the compilation, and the C language proper. All the statements which begin with a hash mark as the first character of a line (such as #include and #define) are pre-processor directives, and they change the source code itself before the compiler actually begins running. For example, the #include directive tells the program to copy the text of another file into the source code stream, verbatim.

What happens when you use #define is that it creates a name for the string of code that follows the first word, so, if you have a statement
Code:
`#define BUFFER_SIZE 127`

Then wherever BUFFER_SIZE appears in the code, the pre-processor will replace it with 127. This is not a variable or constant declaration; the exact string is replaced, before the compiler itself gets to see the code. So, if you had written the macro as
Code:
`#define BUFFER_SIZE 128 - 1`

it would insert the exact string '128 -1' into the source code stream. This can cause some problems at times, for example, if you wrote:
Code:
`a = BUFFER_SIZE * 16;`

it would actually convert to
Code:
`a = 128 - 1 * 16;`

which results as 112 at runtime rather than 2048 as you would probably expect. This is part of why instructors in C today tend to shy away from the pre-processor macros. You generally can avoid such side-effects by putting the whole macro body in parentheses, but even this isn't foolproof.

With a parameterized macro, like your example of haszero(), the parameter is substituted for the arguments given it, so that
Code:
```#define haszero(v) (((v) - 0x01010101UL) & ~(v) & 0x80808080UL)
/* ...some code later ... */
a = haszero(10);```

would produce
Code:
`a =  (((10) - 0x01010101UL) & ~(10) & 0x80808080UL)`

(I'll leave the evaluation of this up to you.)

As for the 'return type', it doesn't strictly return a value the way a function does; rather, the statement is inserted into the code as it is. In this case, the result is to compute whether there are any zero bits in the number below the highest set bit. It results in either a value of zero, if there aren't, or a non-zero number if there are. Since C treats all non-zero values as true, it can be used to test the number in question.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

#8
November 19th, 2012, 08:30 PM
 C505
Registered User

Join Date: Nov 2012
Posts: 23
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0
Thanks so much for that reply. With that I was able to finish my function, and it even works.

Im still having some issues with the finished product, but my code is much better than it was a few days ago thanks to the help I got here.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Bit_test function

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap