| | |
Problems with bitwise operators
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
Hi,
I am having problem with the following code.This is on a 32 bit system. The problem statement is:
/* isLess - if x < y then return 1, else return 0
* Example: isLess(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
*/
I can't use any loops or - * / !! || < >
My code so far is:
int isLess(int x, int y) {
return (( x + (~y + 1)) >> 31 ) & 1 ;
}
It works for all numbers except for negative numbers:
Test isLess(2147483646[0x7ffffffe],-2[0xfffffffe]) failed.
Gives 1[0x1]. Should be 0[0x0]
I am new to programming and am very green when it comes to the negative numbers!
Any help is appreciated!
I am having problem with the following code.This is on a 32 bit system. The problem statement is:
/* isLess - if x < y then return 1, else return 0
* Example: isLess(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
*/
I can't use any loops or - * / !! || < >
My code so far is:
int isLess(int x, int y) {
return (( x + (~y + 1)) >> 31 ) & 1 ;
}
It works for all numbers except for negative numbers:
Test isLess(2147483646[0x7ffffffe],-2[0xfffffffe]) failed.
Gives 1[0x1]. Should be 0[0x0]
I am new to programming and am very green when it comes to the negative numbers!
Any help is appreciated!
![]() |
Similar Threads
- Enquiries about Bitwise operators (C++)
- Question Regarding "Bitwise Operators in C" (C)
- Diff b/w logical operators and bitwise operators...? (C)
Other Threads in the C Forum
- Previous Thread: Why use Dynamic and Static Memory Allocation...
- Next Thread: Error message help
Views: 4409 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






