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

comparing msb


I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b. Right now the code
I am using looks like this

int w1,w2;
asm("bsr %0,%1" : "=r" (w1) : "r" (a));
asm("bsr %0,%1" : "=r" (w2) : "r" (b));
return w1 < w2;

Anyone has any better ideas on how to do this. Seems
like this code is pretty slow. Is there an easier way
to compare the msb of two numbers? Maybe using one
assembly command? Or maybe by doing something else
compared to bsr?

Thanks,
--j

Nov 14 '05 #1
5 1988
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In comp.lang.c, John wrote:
I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b. Right now the code
I am using looks like this

int w1,w2;
asm("bsr %0,%1" : "=r" (w1) : "r" (a));
Oops. This doesn't compile because of a syntax error.
Plus, there's no standard C function called asm()
asm("bsr %0,%1" : "=r" (w2) : "r" (b));
See above
return w1 < w2;

Anyone has any better ideas on how to do this.


With a bit better definition of your requirements, yes.

[snip]
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCXVVyagVFX4UWr64RAtEwAKDznc5u6qns4WvqdSxLr7 grI0l/5wCfVu2a
pPSZDi/y43XOmNO9kxdrdNc=
=00Iy
-----END PGP SIGNATURE-----

Nov 14 '05 #2
"John" <sp******@crayne.org> writes:
I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b.


If a and b are unsigned, then a > b is pretty close. If I am
thinking correctly, it can only be wrong if a and b have the same
MSB. Does that help?

If a and b are signed, then I'm not sure what qualifies as the
MSB.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery

Nov 14 '05 #3
John wrote:
I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b. Right now the code
I am using looks like this

int w1,w2;
asm("bsr %0,%1" : "=r" (w1) : "r" (a));
asm("bsr %0,%1" : "=r" (w2) : "r" (b));
return w1 < w2;

Anyone has any better ideas on how to do this. Seems
like this code is pretty slow. Is there an easier way
to compare the msb of two numbers? Maybe using one
assembly command? Or maybe by doing something else
compared to bsr?

Thanks,
--j


The safest method is to extract the most significant
bits and compare them. This becomes a bit tricky
with different bit-widths of variables and also
for Endianism.

In a header file, <limits.h>, is a symbol CHAR_BIT,
which defines the number of bits in the type char.
The "sizeof" operator will return the number of
units (of type char) of a type or structure.
Knowing this, the number of bits in an integer
can be found.

Let's define the most significant bit (MSB) as:
msb = 1 << ((sizeof(int) * CHAR_BIT) - 1)
which is a 1 left shifted to one less than the
number of bits in an integer.

The MSB can be isolated using this information
and the bitwise AND operator.

However, setting up the relationship in a truth
table yields:
---------+----------+-------
MSB of A | MSB of B | A > B
---------+----------+-------
0 | 0 | False (0)
---------+----------+-------
0 | 1 | False (0)
---------+----------+-------
1 | 0 | True (1)
---------+----------+-------
1 | 1 | False (0)
---------+----------+-------

result = msb_a AND NOT msb_b.

The implementation of this is left as an exercise
for the O.P.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #4
"Ben Pfaff" <sp******@crayne.org> wrote in message
news:87************@benpfaff.org...
"John" <sp******@crayne.org> writes:
I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b.


If a and b are unsigned, then a > b is pretty close. If I am
thinking correctly, it can only be wrong if a and b have the same
MSB.


I think you're thinking correctly. I also think the exclusive-or operator
can help you figure out the case where a and b have the same MSB.

Alex
Nov 14 '05 #5
Thomas Matthews wrote:

John wrote:
I have two numbers a,b.

I would like to compare if most significant bit of a is
larger than most significant bit of b. Right now the code
I am using looks like this

int w1,w2;
asm("bsr %0,%1" : "=r" (w1) : "r" (a));
asm("bsr %0,%1" : "=r" (w2) : "r" (b));
return w1 < w2;

Anyone has any better ideas on how to do this. Seems
like this code is pretty slow. Is there an easier way
to compare the msb of two numbers? Maybe using one
assembly command? Or maybe by doing something else
compared to bsr?

Thanks,
--j


The safest method is to extract the most significant
bits and compare them. This becomes a bit tricky
with different bit-widths of variables and also
for Endianism.

In a header file, <limits.h>, is a symbol CHAR_BIT,
which defines the number of bits in the type char.
The "sizeof" operator will return the number of
units (of type char) of a type or structure.
Knowing this, the number of bits in an integer
can be found.

Let's define the most significant bit (MSB) as:
msb = 1 << ((sizeof(int) * CHAR_BIT) - 1)
which is a 1 left shifted to one less than the
number of bits in an integer.

The MSB can be isolated using this information
and the bitwise AND operator.


Wrong.
1 << ((sizeof(int) * CHAR_BIT) - 1)
is implementation defined if int has no padding.

If int has any padding then it's especially wrong.

A portable expression for an unsigned number
with only the most significant bit set is:
unsigned mask = ((unsigned)-1 >> 1) + 1;

--
pete

Nov 14 '05 #6

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

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
3
by: Ricky W. Hunt | last post by:
How does VB.NET determine comparing vs. assigning? For instance, if "checkbox1.checked = True" it only checks the value but leaves it as it whereas if you have "checkbox1.checked = True" by...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
4
by: Frank | last post by:
Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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,...
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
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.