473,804 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i compare bit wise?

Hi everyone,
I thought of writing a program which finds the greater of two numbers..
of course with a small condition... without using "if" and conditional
operators... I got an idea on how to implement the program... I have an
alternative too... that is to use bitwise comparison in the folowing
manner:
Accept the input
Subtract one number from the other
Check for the MSB(Most Significant Bit) in the result...
See if the result is positive or negative...
If negative then the second number is bigger...

Though i could think of the algorithm, i am unable to write a code in a
simple manner which can check the Most significant bit of the result...
How can do it...?

By the way, this was the logic i used for a simple solution:

/*The usual C statements*/
max=a+b+abs(a-b);
/*End of Program after the usual formalities*/

I just wanted to know how to test a single bit... whether it is
positive or negative i.e. 0 or 1...

And finally, is there any way i could print an integer in its binary
form directly without writing a program for that?

Nov 14 '05 #1
14 15558
Oops...got the answer on how to check for the last bit..

x=number>>(size of(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i
can print binary form of an integer directly?

Nov 14 '05 #2
"purifier" <cn******@gmail .com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Oops...got the answer on how to check for the last bit..

x=number>>(size of(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i
can print binary form of an integer directly?


There is no built-in facility or function, but
writing one is not difficult. As a matter of
fact, if you search the archives of clc you should
find examples in code that I and others have
posted here in the past.

-Mike
Nov 14 '05 #3

Mike Wahler wrote:
"purifier" <cn******@gmail .com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Oops...got the answer on how to check for the last bit..

x=number>>(size of(int)*8-1);

and then checking if x is 0 or non zero...

However the second part remains a puzzle to me... Is there any way i can print binary form of an integer directly?


There is no built-in facility or function, but
writing one is not difficult. As a matter of
fact, if you search the archives of clc you should
find examples in code that I and others have
posted here in the past.

-Mike


Thanks Mike...
Actually i did write a program myself... but i was wondering if C
already had a built in function... anyway thanks for the
clarification.. .

Nov 14 '05 #4
"purifier" <cn******@gmail .com> writes:
Hi everyone,
Please stop creating new threads. Unless you have a separate question
(which you don't in this case), post a followup to the previous
thread.
I thought of writing a program which finds the greater of two numbers..
of course with a small condition... without using "if" and conditional
operators...
What exactly do you mean by "conditiona l operators"?
I got an idea on how to implement the program... I have an
alternative too... that is to use bitwise comparison in the folowing
manner:
Accept the input
Subtract one number from the other
Check for the MSB(Most Significant Bit) in the result...
See if the result is positive or negative...
If negative then the second number is bigger...
Subtraction can overflow. For example, if int is a 16-bit
2's-complement type (range -32768 .. +32767), subtracting -30000 from
+30000 yields 60000, which doesn't fit in an int. Even if you assume
some specific behavior on overflow, you're not going to get consistent
results.

[...]
And finally, is there any way i could print an integer in its binary
form directly without writing a program for that?


No. The *printf() functions support decimal, octal, and hexadecimal,
but not binary. But it's easy enough to implement it yourself.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
"purifier" <cn******@gmail .com> writes:
Oops...got the answer on how to check for the last bit..

x=number>>(size of(int)*8-1);

and then checking if x is 0 or non zero...


What is the significance of the number 8? (Hint: CHAR_BIT, the number
of bits in a byte, isn't necessarily 8.)

If number is negative, the result of a right-shift operator is
implementation-defined.

You're also assuming that int has no padding bits (i.e., that you can
determine the number of significant bits from sizeof(int)). That's
true on most systems, but it's not guaranteed.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
purifier wrote:
Oops...got the answer on how to check for the last bit..

x=number>>(size of(int)*8-1);

and then checking if x is 0 or non zero...
That only works if 'number' is an unsigned int .
Right-shifts of a signed value are implementation-defined
as to whether the new bits are 0 or 1.

You could improve it slightly:

.. x = 1UL & (number >> sizeof number * CHAR_BIT - 1);

then it will work for any integral type (except for long long).

BTW, checking for the MSB does not necessarily indicate
positive or negative (although MSB = sign-bit is a very
common implementation) . The easiest way to check if
x is negative is: (x < 0)

However the second part remains a puzzle to me... Is there any way i
can print binary form of an integer directly?


You point to it with an (unsigned char *), and then
printf with "%02x" for each byte (if you want hex output).

Actually I have a similar problem to yours in some real code.
The poxy compiler I have to use, issues a compile-time warning
if a conditional is always true or always false, and there
is no way to turn that specific warning off. So I have
a macro:

#define IS_LT(x,y) (!!( ((ulong)(x) - (y)) & 0x8000000UL ))

that is non-zero if x < y . This macro does rely on some things
specific to my implementation (eg. size of ulong, and defined
behaviour on int overflow, in case x and y are signed int/long).
I don't entirely trust this macro; I only use it for
STATIC_ASSERTs.

Nov 14 '05 #7
/*I write a simple routine just now*/
void int2bin(int ori)
{
int mask=0x01;
int len=sizeof(int) *8-1;
printf("%d",len );
while(len>-1)
{
printf("%d",(or i&(mask<<len--))?1:0);
}
}
/*well,anyhow,It works^_~*/

Nov 14 '05 #8
On 19 Jan 2005 08:25:53 -0800, "purifier" <cn******@gmail .com> wrote
in comp.lang.c:
Hi everyone,
I thought of writing a program which finds the greater of two numbers..
of course with a small condition... without using "if" and conditional
operators... I got an idea on how to implement the program... I have an
alternative too... that is to use bitwise comparison in the folowing
manner:


Bit-wise is either poorly defined or undefined for signed integer
types, and useless for floating point types.

#include <stdio.h>
#include <limits.h>

int compare(int i1, int i2)
{
return (i1 >= i2) * i1 + (i1 < i2) * i2;
}

int main(void)
{
printf("greater of %d and %d is %d\n",
6, 12, compare(6, 12));
printf("greater of %d and %d is %d\n",
INT_MAX, INT_MIN, compare(INT_MAX , INT_MIN));
return 0;
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
u can also mask the bits appropriately using the logical operators
instead of shifting ......

Nov 14 '05 #10

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

Similar topics

5
2247
by: pattanawadee | last post by:
Dear all, I have a problem about compare the 2 variables by use operator 'AND' while one variable is the int constant 0x00000002 and the other one is string '0x202' for the constant 0x00000002 is the flag of systemcall OPEN following fcntl.h #define O_RDWR 0x00000002 /* open for reading and
3
1691
by: Grant Rettke | last post by:
Hi folks, I'm currently evaluating a .NET installer platform. Basically, it has come down to either Wise or Installshield. It would be great to hear about your experiences with these product in deploying .NET apps. If you have experience deploying MSDE and the .NET runtime along with your app, that would be great.
5
37874
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
4
1685
by: Chuck Cobb | last post by:
Can someone tell me how to do a bit-wise "And" in VB.Net - I've looked all over and can't find it. I thought the "And" in VB6 was replaced by a "BitAnd" function, but I can't find it anywhere and when I try to use it, it is unrecognized. Is it part of a class somewhere? Thanks, Chuck Cobb
5
755
by: Enrique Cruiz | last post by:
Hello all, I am currently implementing a fairly simple algorithm. It scans a grayscale image, and computes a pixel's new value as a function of its original value. Two passes are made, first horizontally and second vertically. The problem I have is that the vertical pass is 3 to 4 times slower than the horizontal, although the code is _exactly_ the same in both cases?! The code itself is very simple. There are 2 loops to scan through...
1
1091
by: kannabiran | last post by:
Hi, Im using vb.net as a front end and oracle as a back end,have to compare the two database object type and object name if its available means i need as true else false and should be entered in the isavail column... Eg.. obj_type obj_name isavail ------------ --------------- --------- package package_name index Index_name like wise im having in the datagrid,then...
6
6842
by: yinglcs | last post by:
Hi, i have 2 files which are different (1 line difference): $ diff groupresult20070226190027.xml groupresult20070226190027-2.xml 5c5 < x:22 y:516 w:740 h:120 area: --- But when I use the cmp() function to compare 2 files, it return "1",
50
20344
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
0
1263
by: ARC | last post by:
Hello all, I currently have an old version of Wise Install Builder version 7, and in getting ready to go with Access 2007 installs with the Sagekey runtime script, I really don't want to continue using the old Wise installer, that had the famous blue box when installing. I want something more modern, however... In looking at the site for Wise, they only offer Wist Installation Studio as an upgrade path, and the upgrade price IS cost...
0
9594
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,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9171
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, 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...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
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...

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.