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
4 7977 JosAH 11,448
Recognized Expert MVP
(x^x-1)+1>>1
no comments though: figure it out.
kind regards,
Jos
(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!
JosAH 11,448
Recognized Expert MVP
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
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))
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
|
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
infrastructure at said site. DB2 UDB continually is releasing
performance enhancements with each release especially with the most
recent 8.1 and 8.2 Lots of new features introduced and many to aid in
tuning performance. With that said I have to...
|
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., int_fast32_t
4) integer capable of holding a pointer, intptr_t
5) widest integer in the implementation, intmax_t
Is there a valid motivation for having both int_least and int_fast?
|
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
|
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.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
| |
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" have invented.
It just sounds like so much trivial nitpickery that it's hard to
believe it's as common as we've come to believe.
|
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 show a specific number of significant digits to the right of
the decimal place with a format string? NOT number of decimal places,
significant digits.
For example;
|
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 how many of these are significant.
In C for instance,
<quote>
63 significant initial characters in an internal identifier or a macro
name (each universal character name or extended source character is
considered a single character)
— 31...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |