| | |
Bit shifting and masking.
![]() |
•
•
Join Date: Mar 2009
Posts: 11
Reputation:
Solved Threads: 0
I was given homework to write a program that finds if a double (64 bit) is negative. The double uses twos complement, so the first bit will tell if it is positive (0) or negative (1), but you all probably know that.
The problem I am having is that there can be no floating point operations, just bit masking and shifting. The function can be written with no if statements. It should return 1 if it is negative and 0 if it is not. I thought about masking just the sign bit. I also thought about shifting the sign bit all the way through. Is there any way to just access the first bit and use that in the return statement.
Thanks for any help!
The problem I am having is that there can be no floating point operations, just bit masking and shifting. The function can be written with no if statements. It should return 1 if it is negative and 0 if it is not. I thought about masking just the sign bit. I also thought about shifting the sign bit all the way through. Is there any way to just access the first bit and use that in the return statement.
Thanks for any help!
-1
#3 Oct 20th, 2009
decalre a union with one double value and char array of 8 bytes.
in Little Endian format , always the sign will be MSB.
so take 7th array, and perform AND operation with 1(char) left shifted 7 times.
and return the resultant value.
NOTE: the returned value is not 1 here but is 128 in case of -Ve.
not sure its efficient.
AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.
Please correct me if i am wrong.
i'm too eager to know, other efficient methods.
below code is just an example .
in Little Endian format , always the sign will be MSB.
so take 7th array, and perform AND operation with 1(char) left shifted 7 times.
and return the resultant value.
NOTE: the returned value is not 1 here but is 128 in case of -Ve.
not sure its efficient.
AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.
Please correct me if i am wrong.
i'm too eager to know, other efficient methods.
below code is just an example .
C Syntax (Toggle Plain Text)
int getsign( ); union sign { double d1; char d[8]; }u; int main() { u.d1 = -16.32; if(getsign()) printf("\n %lf is -Ve\n",u.d1); else printf("\n %lf is +Ve\n",u.d1); u.d1 = 16.32; if(getsign()) printf("\n %lf is -Ve\n",u.d1); else printf("\n %lf is +Ve\n",u.d1); return 0; } int getsign(double d1) { char m=1; return m<<7 & u.d[7]; }
Last edited by Gaiety; Oct 20th, 2009 at 1:20 am. Reason: return 0;
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
0
#4 Oct 20th, 2009
As requested, Gaiety:
Way too complex for a new programmer...
Not true. What is true is ANDing with a float's maintissa or exponent and getting a useful result is difficult to impossible, but the sign bit is a defined bit in a defined location so it's perfectly valid. If I'm wrong, please point (link) me to the rules & explanation.
Done...
•
•
•
•
decalre a union with one double value and char array of 8 bytes.
in Little Endian format , always the sign will be MSB.
so take 7th array, and perform AND operation with 1(char) left shifted 7 times.
and return the resultant value.
NOTE: the returned value is not 1 here but is 128 in case of -Ve.
not sure its efficient.
•
•
•
•
AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.
•
•
•
•
Please correct me if i am wrong.
i'm too eager to know, other efficient methods.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
0
#5 Oct 20th, 2009
•
•
•
•
As requested, Gaiety:
Way too complex for a new programmer...
Not true. What is true is ANDing with a float's maintissa or exponent and getting a useful result is difficult to impossible, but the sign bit is a defined bit in a defined location so it's perfectly valid. If I'm wrong, please point (link) me to the rules & explanation.
Done...
do we need to use pointer stuff..!
C Syntax (Toggle Plain Text)
int main() { double nums[] ={16.32,-16.32}; int i=0; while(i<2) { if(getsign(&nums[i])) printf("%lf is -ve\n",nums[i]); else printf("%lf is +ve\n",nums[i]); i++; } return 0; } int getsign(double *d1) { char *p; p=(char *)d1; p=p+7; return *p & 1<<7; }
Last edited by Gaiety; Oct 20th, 2009 at 6:16 am. Reason: code added
Minds are like parachutes - they only work when they are open
Gaiety
Gaiety
0
#6 Oct 20th, 2009
Mmm ... interesting. I always thought that bitwise operators were only valid with integral operands. The only way I can think of using bitwise operators with floats/doubles is to use casting pointers. Perhaps something like:
But then, this may also be too complex for a new programmer.
C Syntax (Toggle Plain Text)
int isNegative(float f) { int *ip = (int*)(&f); return (*ip & 0x80000000 ? 1 : 0); }
Last edited by yellowSnow; Oct 20th, 2009 at 10:11 am.
Manic twiddler of bits
0
#8 Oct 21st, 2009
•
•
•
•
Can you explain this last line. I understand the bit wise 'and' to isolate the bit, but I am lost on the last line. (return statement)
return (*ip & 0x80000000 ? 1 : 0);
Thanks for your help
http://cprogramminglanguage.net/c-ternary-operator.aspx
Essentially it is a short form equivalent of the if-else construct. I posted it this way because your original post stated that you weren't permitted to use any 'if' type statements.
Have a read and post back if you still have questions.
Last edited by yellowSnow; Oct 21st, 2009 at 6:01 am.
Manic twiddler of bits
![]() |
Similar Threads
- Bit shifting. (C++)
- Need Some Bit Twiddling Help on File Post Processing (C++)
- Using binary operators! (C)
- How do I Take in Hexidecimal input out put binary, decimal, and octal (Java)
Other Threads in the C Forum
- Previous Thread: Linear canonical transform
- Next Thread: eof
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






