Bit shifting and masking.

Reply

Join Date: Mar 2009
Posts: 11
Reputation: boydale1 is an unknown quantity at this point 
Solved Threads: 0
boydale1 boydale1 is offline Offline
Newbie Poster

Bit shifting and masking.

 
0
  #1
Oct 19th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #2
Oct 20th, 2009
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training
 
-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 .

  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;
Minds are like parachutes - they only work when they are open
Gaiety
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei
 
0
  #4
Oct 20th, 2009
As requested, Gaiety:
Originally Posted by Gaiety View Post
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...

Originally Posted by Gaiety View Post
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.

Originally Posted by Gaiety View Post
Please correct me if i am wrong.
i'm too eager to know, other efficient methods.
Done...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training
 
0
  #5
Oct 20th, 2009
Originally Posted by WaltP View Post
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
Minds are like parachutes - they only work when they are open
Gaiety
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training
 
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:

  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.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: boydale1 is an unknown quantity at this point 
Solved Threads: 0
boydale1 boydale1 is offline Offline
Newbie Poster
 
0
  #7
Oct 20th, 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training
 
0
  #8
Oct 21st, 2009
Originally Posted by boydale1 View Post
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.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC