473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

least significant set bit

Hello,

I'm trying to find the least significant set bit (i.e. the first '1' bit in an int) through just bit twiddling, but I can't get it without resorting to a loop. My original idea was to reverse the bits in an int and find the most significant bit then subtract it from 31 to get the position of the LSB. Is there a way to get the LSB of an int using just bit manipulations?

e.g., 0b0101 would return 0001,
0b0000 would return 0000,
0b1110 would return 0010.


Thanks
Aug 13 '07 #1
4 7958
JosAH
11,448 Expert 8TB
(x^x-1)+1>>1

no comments though: figure it out.

kind regards,

Jos
Aug 13 '07 #2
(x^x-1)+1>>1

no comments though: figure it out.

kind regards,

Jos
Hmm, thanks for the help, but it seems that this solution breaks when it encounters 0x80000000 - instead of returning itself, it returns 0! Most likely since it relies on the fact that (x ^(x-1) + 1 will place the LSB one 'place' up and then shifts it back down; MIN_INT ^ (MIN_INT - 1) results in -1 and adding one makes that 0...

Edit: Ok, i figured out a better way once i worked through why your algotithm works and breaks for MIN_INT ... Thanks for your help!
Aug 13 '07 #3
JosAH
11,448 Expert 8TB
Edit: Ok, i figured out a better way once i worked through why your algotithm works and breaks for MIN_INT ... Thanks for your help!
You're welcome of course; what did you do to make it work for 0x80000000 as well?

kind regards,

Jos
Aug 13 '07 #4
You're welcome of course; what did you do to make it work for 0x80000000 as well?

kind regards,

Jos
(x & -x) which is equivalent to (x & (~x + 1))
Aug 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Spencer | last post by:
One of the things that I have become interested in lately is questioning in general what percentage of DB2 UDB implementations are configured to run at least near optimally given the hardware...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
52
by: onsbomma | last post by:
I want to set and reset the least significant bit of a address (where a pointers points to). I tried this, but it is not correct: #define BIT 0x1 void foo(){ void *p; *p = *p & ~BIT
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like a mythical caste of tasteless heathens that "we"...
7
by: Michael Howes | last post by:
MSDN documentation for format strings seems ambiguous. It's says things like "significant digits or number of decimal places" all over the place. Well those two things are different. How do I...
4
by: robokevs009 | last post by:
Hello everyone, I'm new to PERL and i just wanted to ask how many are the significant initial characters in naming a variable in PERL. I know that a variable has a maximum name length of 255, but...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.