Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-10-2005, 11:11 PM   #1 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default Bitwise problems

Hey, I'm doing a homework problems and am very stuck.

This is your basic bitwise operator problem.

I have this one working for small values, but when they approach the maximum 2's compliment size, they return the wrong value.
Code:
/*
 * fitsBits - return 1 if x can be represented as an
 *  n-bit, two's complement integer.
 *   1 <= n <= 32
 *   Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 15
 *   Rating: 2
 */
int fitsBits(int x, int n)
{


  int shift = 1 << n;
  int test = (shift ^ (x << 1)) >> n;

  return !!test;


}
If anyone's a guru at these problems, could you give me a hand. Thare are other's that I'm working on also, but I am very lost.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 02-16-2005, 05:52 PM   #2 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

This is how i did it:

Code:
#include <iostream>

using namespace std;
int fitsBits(int x, int N);
int main()
{
	int num;
	int bits;
	
	cout<<"Enter the number and bit length: ";
	cin>>num>>bits;
	cout<<endl;
	cout<<fitsBits(num, bits);
	
	return 0;
}

int fitsBits(int x, int N) 
{ 
 int fits = - (x & (1 << N - 1)); 
 for(int i = N - 2; i >= 0; --i) 
 {
 	fits |= (x & (1 << i));
 }
 
 if(fits == x)
 {
 	return 1;
 }
 else
 {
 	return 0;
 }
}

__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 02-16-2005, 07:50 PM   #3 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Awesome. I can't acutally use looping or conditional structures, but this gives me a better idea of how to go about it.

Thanks!
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 02-18-2005, 01:12 AM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

I finally got it, if you're curious at all.

Code:
int fitsBits(int x, int n)
{
   mask = x >> 31;
   size = n + ~0x0;

   tmp1 = ~x & mask;
   tmp2 = x & ~mask;

   return !((tmp1 + tmp2) >> size);

}
Supposedly you can do it in only 6 operations, but I'm still baffled by that. This one takes 10 operations.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 02-18-2005, 02:14 AM   #5 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Yeah it looks good man, maybe i'll try and see if i can get it working in six operations.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 02-19-2005, 02:49 AM   #6 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Finally got the version in 6 operations.

It seems so obvious once you figure it out. Stupid computers!

Code:
int fitsBits(int x, int n)
{
  int shift = 33 + ~n;
  int x2 = x << shift;
  int x3 = x2 >> shift;

  return !(x3 ^ x);
}

__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 02-19-2005, 03:00 PM   #7 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Excellent man, i tested it and it works great.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On