473,397 Members | 1,960 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,397 software developers and data experts.

x&(x-1) ... why only 2's complement system?

Lax

Why is the "x&(x-1)" trick for removing the least significant set bit
from an integer only valid on 2's complement systems?

Apr 9 '08 #1
6 2138
Lax wrote:
Why is the "x&(x-1)" trick for removing the least significant set bit
from an integer only valid on 2's complement systems?
It is valid on all systems if `x' is non-negative.
That means, as a particularly useful special case, that
it is always valid if `x' is unsigned.

For negative values of `x', I suggest you take pencil
and paper and work through a few examples, using all three
of the representations C allows: two's complement, ones'
complement, and signed magnitude. To save some writing,
pretend your computer uses a narrow int of maybe six or
eight bits. Try a few different `x' values like -1, -2,
-10, and see what happens.

--
Er*********@sun.com
Apr 9 '08 #2
Lax
On Apr 9, 5:09*pm, Lax <Lax.Cla...@gmail.comwrote:
Why is the "x&(x-1)" trick for removing the least significant set bit
from an integer only valid on 2's complement systems?
Thank you all (for suggesting and showing counterexamples).

So is this a good implementation-independent way of doing it?

(signed)( x & ( (unsigned)x-1 ) )

Apr 9 '08 #3
Lax wrote:
Lax <Lax.Cla...@gmail.comwrote:
Why is the "x&(x-1)" trick for removing the least
significant set bit from an integer only valid on 2's
complement systems?

Thank you all (for suggesting and showing
counterexamples).

So is this a good implementation-independent way of doing it?

(signed)( x & ( (unsigned)x-1 ) )
No, the right way is to make x unsigned to begin with, and
to forget about testing bits in negative integers.

--
Peter
Apr 10 '08 #4
Lax wrote:
>
Why is the "x&(x-1)" trick for removing the least significant set
bit from an integer only valid on 2's complement systems?
It works fine on unsigned integers also. Just write down the bit
patterns involved.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Apr 10 '08 #5
Lax wrote:
Lax <Lax.Cla...@gmail.comwrote:
>Why is the "x&(x-1)" trick for removing the least significant set
bit from an integer only valid on 2's complement systems?

Thank you all (for suggesting and showing counterexamples).
So is this a good implementation-independent way of doing it?

(signed)( x & ( (unsigned)x-1 ) )
NO. It is a good way of getting undefined behaviour.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Apr 10 '08 #6
Eric Sosman wrote:
Lax wrote:
>Why is the "x&(x-1)" trick for removing the least significant set bit
from an integer only valid on 2's complement systems?

It is valid on all systems if `x' is non-negative.
That means, as a particularly useful special case, that
it is always valid if `x' is unsigned.

For negative values of `x', I suggest you take pencil
and paper and work through a few examples, using all three
of the representations C allows: two's complement, ones'
complement, and signed magnitude. To save some writing,
pretend your computer uses a narrow int of maybe six or
eight bits. Try a few different `x' values like -1, -2,
-10, and see what happens.
Hmm. I stand corrected.

--
--Falcon Kirtaran
Apr 10 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mantorok Redgormor | last post by:
From least to greatest is it sign magnitude ones complement two's complement Where sign magnitude is the least way to represent integers and two's complement is the best way to represent...
33
by: Daniel Fadlun | last post by:
Is there a bigger, mathematical, data type in C than the double (64 bit) one or the old long double (80 bit)? I'd like to add precision to my mathematical application, but I can't figure out how....
7
by: Greenhorn | last post by:
Hi, Is two's complement always used as a storage method or is it computed while computing the expression involved. e.g., int a = -2, b = 3, c = 4, d; d = b - c; Here, is 'a' stored as two's...
3
by: Frederick Gotham | last post by:
(I'm not sure if there's already something in the Standard Library for doing this... ?) Is the following macro okay for getting a compile-time constant that indicates which negative number...
22
by: sarathy | last post by:
Hi all, I have a few doubts in the 1's and 2's complement representation. Generally negative numbers can be represented using either 1's complement or 2's complement representation. 1's...
14
by: darthghandi | last post by:
What would be the most efficient way to calculate the two's complement of a variable length byte array? Thanks for your time.
3
by: vijaybaskar3108 | last post by:
hi, I just want to know how to find complements for a number. These are the following answers for complements 2's complement(10110)=01010 4's complement(1230)=2110 5's complement(4322)=0123...
6
by: Dan Henry | last post by:
I need a sanity check. The following is an exchange on comp.arch.embedded with CBFalconer in a rather long MISRA related thread. Since my little section of that thread and area of interest was...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.