C Programming
 
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.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 18th, 2012, 12:38 PM
C505 C505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 23 C505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)
{
}

Reply With Quote
  #2  
Old November 18th, 2012, 12:49 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.

Reply With Quote
  #3  
Old November 18th, 2012, 01:04 PM
C505 C505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 23 C505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old November 18th, 2012, 01:17 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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 :-)

Reply With Quote
  #5  
Old November 18th, 2012, 01:32 PM
C505 C505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 23 C505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 34 m 7 sec
Reputation Power: 0
Post

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?

Reply With Quote
  #6  
Old November 19th, 2012, 08:45 AM
C505 C505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 23 C505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #7  
Old November 19th, 2012, 10:31 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
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

Reply With Quote
  #8  
Old November 19th, 2012, 08:30 PM
C505 C505 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 23 C505 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bit_test function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

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