943,946 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1401
  • C RSS
Oct 19th, 2009
0

Bit shifting and masking.

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boydale1 is offline Offline
11 posts
since Mar 2009
Oct 20th, 2009
0
Re: Bit shifting and masking.
0x8000000000000000 is the 'sign bit' of a 64 bit value. AND it with the value you want to check. If TRUE, the sign bit is set.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Oct 20th, 2009
-1
Re: Bit shifting and masking.
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 .

  1. int getsign( );
  2. union sign {
  3. double d1;
  4. char d[8];
  5. }u;
  6.  
  7. int main()
  8. {
  9. u.d1 = -16.32;
  10.  
  11. if(getsign())
  12. printf("\n %lf is -Ve\n",u.d1);
  13. else
  14. printf("\n %lf is +Ve\n",u.d1);
  15.  
  16. u.d1 = 16.32;
  17.  
  18. if(getsign())
  19. printf("\n %lf is -Ve\n",u.d1);
  20. else
  21. printf("\n %lf is +Ve\n",u.d1);
  22.  
  23. return 0;
  24. }
  25.  
  26. int getsign(double d1)
  27. {
  28. char m=1;
  29. return m<<7 & u.d[7];
  30. }
Last edited by Gaiety; Oct 20th, 2009 at 1:20 am. Reason: return 0;
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
113 posts
since Sep 2009
Oct 20th, 2009
0
Re: Bit shifting and masking.
As requested, Gaiety:
Click to Expand / Collapse  Quote originally posted by Gaiety ...
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.
Way too complex for a new programmer...

Click to Expand / Collapse  Quote originally posted by Gaiety ...
AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.
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.

Click to Expand / Collapse  Quote originally posted by Gaiety ...
Please correct me if i am wrong.
i'm too eager to know, other efficient methods.
Done...
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Oct 20th, 2009
0
Re: Bit shifting and masking.
Click to Expand / Collapse  Quote originally posted by WaltP ...
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...
could you please provide me a small code snippet on how that can be done?
do we need to use pointer stuff..!

  1.  
  2. int main()
  3. {
  4. double nums[] ={16.32,-16.32};
  5. int i=0;
  6. while(i<2)
  7. {
  8. if(getsign(&nums[i]))
  9. printf("%lf is -ve\n",nums[i]);
  10. else
  11. printf("%lf is +ve\n",nums[i]);
  12. i++;
  13. }
  14.  
  15. return 0;
  16. }
  17.  
  18. int getsign(double *d1)
  19. {
  20. char *p;
  21. p=(char *)d1;
  22. p=p+7;
  23. return *p & 1<<7;
  24. }
Last edited by Gaiety; Oct 20th, 2009 at 6:16 am. Reason: code added
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
113 posts
since Sep 2009
Oct 20th, 2009
0
Re: Bit shifting and masking.
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:

  1. int isNegative(float f) {
  2. int *ip = (int*)(&f);
  3. return (*ip & 0x80000000 ? 1 : 0);
  4. }
But then, this may also be too complex for a new programmer.
Last edited by yellowSnow; Oct 20th, 2009 at 10:11 am.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Oct 20th, 2009
0
Re: Bit shifting and masking.
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boydale1 is offline Offline
11 posts
since Mar 2009
Oct 21st, 2009
0
Re: Bit shifting and masking.
Click to Expand / Collapse  Quote originally posted by boydale1 ...
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
The example I posted demonstrates the use of a ternary operator. Here's a brief summary on how the ternary operator works:

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.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Linear canonical transform
Next Thread in C Forum Timeline: eof





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC