### Thread: Bit_test function

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep 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. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34
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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
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. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34
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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep 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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
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. 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.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep 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.

:D