Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-15-2009, 06:13 PM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2009

Posts: 1

Crazy Ivan 6023 is on a distinguished road

Default Finding Log2

hey guys, i was wondering if any one could show me how to find log2 using only bit operators. here is the jist of what i have thus far, but i still am a little off. you have to find the floor of log 2 of an integer

int log2(int x) {
int logg = 0;
int shifted_x=0;
int shift_val=0;
int big_remove=(255<<8)|255;

/*Isolates the first half of the number and determines if there is a one in
the sequence.*/
shifted_x=x>>16;

//Masks the first half of the bits in the last half.
shifted_x=shifted_x&big_remove;
/*If shifted_x is greater than 0, then 16 will be added to the logg and then
stored as the new shift_val. If not, then the shift_val will be 0*/
shift_val+=(!(!shifted_x))<<4;
logg=shift_val;


/*Shifts the x value by shift_val, which is either 16, or 0.*/
shifted_x=x>>shift_val;

/*Masks the resulting shift with the first quarter. The end result determines
if the bits are in the first quarter, or second quarter if x was shifted and
if the bits are in the thrid quarter or the fourth quarter if x was not
shifted.*/
shifted_x=shifted_x&0xFF;

/*Determines the new shift value. If the result is a non-zero, then then that
value is added to the shift. Therefore the next shift will be either 0, 8,
16, or 24.*/
shift_val+= (!(!shifted_x))<<3;
logg=shift_val;



/*Shifts the x value by shift_val, which is either 0, 8, 16, 24.*/
shifted_x=x>>shift_val;

/*Masks that new value to rid isolate 1/8 of the bit sequence.*/
shifted_x=shifted_x&0xF;

/*Determines the new shift value. This value can either be either 0, 4, 8,
12, 16, 20, 24, 28*/
shift_val+=(!(!shifted_x))<<2;
logg=shift_val;


/*Shifts the x value by the new shift_val*/
shifted_x=x>>shift_val;

/*Masks the new shifted_x value which represents 1/16 of the bit sequence.*/
shifted_x=shifted_x&0x3;

/*Determines the new shift value. This value can either be 0, 2, 4, 6, 8, 10,
12, 14, 16, 18, 20, 22, 24, 26, 28, 30*/
shift_val+=(!(!shifted_x))<<1;
logg=shift_val;


/*Shifts the x value by the new shift_val*/
shifted_x=x>>shift_val;

/*Masks the new shifted_x value which represents 1/32 of the bit sequence*/
shifted_x=shifted_x&0x1;

/*Determines the new shift value. This value can be any value in the range
of 0<=shift_val<=31.*/
shift_val+=!(!shifted_x);
logg=shift_val;


/*Now the function returns the found shifted value which found how many shifts
were needed to find the greatest bit.*/
return logg;

Last edited by Crazy Ivan 6023; 09-15-2009 at 06:15 PM.
Crazy Ivan 6023 is offline  
Old 09-16-2009, 08:53 AM   #2 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Finding Log2

This smells like homework to me. Thread closed.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help finding my computer specs bloodymasher Windows Operating Systems and Software 3 08-04-2008 11:18 AM
pc fault finding questions robina_80 Off Topic Discussion 10 07-14-2008 08:30 PM
Need help finding a certain kind of keyboard ThrowingChicken Building, Buying, Upgrading for General PCs 2 03-25-2008 10:17 AM
Need help finding a part. Saxon Everything Laptops 1 09-15-2007 12:17 PM
Need help indentifying and finding a solution to convert this USB wire. Kutabare Hardware Troubleshooting 4 08-03-2007 11:47 PM