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

Bitwise operation challenge!!!

I need an algorithm which compares two numbers by bitwise should tell me if only one bit is different from other among all the bits.

for example
let say my number is 5
5 binary is 101 then the comparisons of
100
111
001
shall all pass the algorithm as there is only one bit change.
Apr 4 '07 #1
3 1410
horace1
1,510 Expert 1GB
you could loop for the number of bits in the word
(1) compare the two bit 0's, if different exit
(2) shift bits right one bit
Apr 4 '07 #2
JosAH
11,448 Expert 8TB
If both numbers a and b differ in only one bit then a^b must be a number x with
only one bit set to one (1). Such a number definitely isn't equal to zero but the
following expression is equal to zero: x&(x-1). For all other pairs of nunbers a
and b the number x= a^b will be zero ot that funny expression will be unequal
to zero.

kind regards,

Jos
Apr 4 '07 #3
If both numbers a and b differ in only one bit then a^b must be a number x with
only one bit set to one (1). Such a number definitely isn't equal to zero but the
following expression is equal to zero: x&(x-1). For all other pairs of nunbers a
and b the number x= a^b will be zero ot that funny expression will be unequal
to zero.

kind regards,

Jos
Beutiful thanks a lot
Apr 8 '07 #4

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

Similar topics

13
by: Patrick Hoonhout | last post by:
Hello, Trying to get the bit offset value from a byte. For example: 0x1 = 0 0x2 = 1 0x4 = 2 0x8 = 3 0x10 = 4 ...
7
by: Jerry | last post by:
I want an algorithm that do arithmetic operations(divide,mutiply,add etc.)just using bitwise operators:<<,>>,&,|,^; For example,how "a/10" can be implemented. I just want a hint. Thanks.
10
by: David R. | last post by:
I want to do bitwise operation on some large integers. For example, Response.Write CBool(2 AND 2^30) ' returns False Response.Write CBool(2 AND 2^31) ' CRASHED! Looks like the AND...
1
by: jerico | last post by:
Hi.Given a number ,say 15, whose binary equivalent is 1111, how can the bit value of any position can be obtained using bitwise operators?Thanks for any help. Jerico
4
by: John Dann | last post by:
I'm trying to generate a 4-byte unsigned integer in VB2005 that encodes a number of bit-level variables. What I need to do is to be able to compute a value for the uint that is based on specific...
4
by: loudking | last post by:
Hello, all. I am not good at bitwise operation, so I wonder if some experts could do me a help. In a.c, somebody defined ================================================ # define u_int64...
3
by: Sakhtkoosh | last post by:
I have written a program in C environment. I need to do operation on codes with more than 64 elements whose values are 0 and 1. At first, I simulated it with an array in 2 dimension(n*m) but after...
3
by: Eman Ahmed | last post by:
Hi,All I'm now working in project to compress a files. And I used Huffman encoding to get new code for each character in the site. but now I can't complete my project. How can I use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.