Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2005, 9:37 AM   #1
scrapper
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 scrapper is on a distinguished road
Angry Problem with bits

Hi,

I am trying to write the a program as follows:

isLess - if x < y then return 1, else return 0. Example: isLess(4,5) = 1.
Legal operations are ! ~ & ^ | + << >>

I cannot use loops or logical operators. This is running on a 32 bit system.

My code so far is:

int isLess(int x, int y) {

return !((y + (~x + 1) ) >> 31) ;
}

It seems to work for all numbers except for negative numbers, example:
Test isLess(1768011938[0x6961b8a2],-2147483644[0x80000004]) failed.
Gives 1[0x1]. Should be 0[0x0]

Does anyone see anything obviously wrong with my code? It's driving me crazy!

Thanks,
scrapper is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC