1. ## Dealing with bits

I had a problem writing the following function:
/*
* lowbit and highbit
* Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
* If lowbit > highbit, then mask should be all 0's
Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/

I wrote this so far:
Code:
```int bitMask(int highbit, int lowbit) {
int PosOrNeg = highbit+(~lowbit+1);
int sign = PosOrNeg >> 31;

int endXOR = sign ^ ~0;

int ANDwith = ~(0x1 << 31);
int rshift = 32 + (~highbit + 1);
ANDwith = ANDwith >> rshift;
ANDwith = ANDwith << lowbit;

endXOR = endXOR & ANDwith;

int num= 0 ^ endXOR;
return num;
}```
I know it is very poor code and it doesn't seem to work when the low bit is 0 and/or high bit is 31 among other cases. Try as I might, I can't seem to be able to fix this problem. I think I'm going about this problem wrong. I know a lot of you guys are pros here so if you could point me in the right direction, many thanks to you!

2. I'm not sure what it is you're trying to do here. If you pass it 5 and 3, are you trying to get:

00000000 00000000 00000000 00111000

IE: Everything from high to low is set to 1 and the rest zero? Or what?

Quzah.

3. I'm probably assuming too much again somehow or other, but you could try
Code:
```unsigned long bitMask(unsigned long high, unsigned long low)
{
unsigned long out, t = ~0;

out = (t << high) << 1;
out |= ~(t << low);

return ~out;
}```
where you essentially create the bit fields at the sides of the desired field, then invert the bits

4. Doesn't work if high < low unfortunately.

5. Oops, I didn't see that part of the specification...

6. I'll try to clarify on the directions. Since the function doesn't pass in an int to bit manipulate, I believe we initially start off with an int set to all 0;s; 32 0's since this code is expected to run on a 32-bit machine (machine is assumed to run 2's-complement BTW). When we pass in a lowbit and a highbit, the bits from lowbit and highbit (inclusive) of the int should now be set to 1. If the lowbit is greater than highbit, then the int should just remain as all 0's

I'm also having some trouble with high<low. The only way I can seem to differentiate it is doing some form of (high + (~low +1)) to subtract them and find out the sign bit. Problem is I don't know what to do with this.

Thanks for the attempts so far guys. I appreciate it.

7. So initialize your variable at start to zero, preform an if check...
Code:
```int foo( int bar, int baz )
{
int fbb = 0;
if( baz < bar )
...play around with the bits...
return fbb;
}```
Quzah.

8. Well this would work on a compiler which pads negative signed ints with 1s when shifted to the right:
Code:
```unsigned long getbitmask(unsigned long high, unsigned long low)
{
unsigned long out, sign, t = ~0;

sign = (signed long)(high + ~low + 1) >> ((sizeof(long) << 3) + t); //or 31 if this is going to be run on a system in which sizeof(long) = 4

out = (t << high) << 1;
out |= ~(t << low);

return ~(out | sign); //equivelent to ~out & ~sign;
}```
quzah:
Legal ops: ! ~ & ^ | + << >>

9. if is not an operator. It is a keyword. if you want to be picky, = is not in the list of allowed operators! You lose!

Quzah.

10. Well I was actually refering to your '<' operator...

And besides, you can still write the function without the assignment operator:
Code:
```unsigned long getbitmask2(unsigned long high, unsigned long low)
{
return ~(((~0 << high) << 1 | ~(~0 << low)) | ((signed long)(high + ~low + 1) >> 31));
}```

11. Ah, well I suppose '<' is an operator after all then. Well good job on doing their homework anyway.

Quzah.

12. I tried your solution SirNot and makes a lot of sense ORing the 3 different conditions. I am passing in signed ints to begin with for the low and high bits so casting the (high + ~low + 1) to a signed int was not needed for me. Thank you SirNot and everyone else.

Oh and quzah, we had about 15 more of these to do, most of which I manage to figure out after a long time (I'm slow). I was just getting stuck on this one last night (brain stopped working I guess) and figured I'd post here. Anyways, thanks for all your help!

13. Another take:
Code:
```#include <stdio.h>
#include "util.h" /* for bits_uint */

/*******************************************************************************
* lowbit and highbit
* Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
* If lowbit > highbit, then mask should be all 0's
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
{
return (((~0 << lowbit) ^ ((~0 << highbit) << 1)) >> lowbit) << lowbit;
}

int main(void)
{
int i = 5, j = 3;
i = 3, j = 5;
return 0;
}

/*******************************************************************************
* Program Output
*/

/*******************************************************************************
* Description:
*    This function explains the other version "in slow motion".
*    It uses the bits_uint function, implemented elsewhere, to show the bits.
*/
{
char binary[33];
int a,b,c,d,e;

a =  ~0 << lowbit;
printf("bitMask_(%d,%d) : a = %s\n", highbit, lowbit, bits_uint(binary, a));

b = (~0 << highbit) << 1;
printf("bitMask_(%d,%d) : b = %s\n", highbit, lowbit, bits_uint(binary, b));

c = a ^ b;
printf("bitMask_(%d,%d) : c = %s\n", highbit, lowbit, bits_uint(binary, c));

d = c >> lowbit;
printf("bitMask_(%d,%d) : d = %s\n", highbit, lowbit, bits_uint(binary, d));

e = d << lowbit;
printf("bitMask_(%d,%d) : e = %s\n", highbit, lowbit, bits_uint(binary, e));

return e;
}```