S
S
sambly2014-03-07 19:52:33
Programming languages
sambly, 2014-03-07 19:52:33

How can you do a bit shift float (double) in C?

It is necessary to make a cyclic shift for the float (double) data type in a given direction, I don’t understand how this is done, I tried through pointers - it doesn’t work. Maybe somehow through union, I tried but it doesn't work...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
werktone, 2014-03-08
@werktone

Rotate bit representation or number itself? If the latter, then it is trite division / multiplication. And if the first, then yes, it is possible through union. If you have double and long long of the same size on your machine, then this code will work:

#include <stdio.h>
union double_long_long 
{
  double dbl;
  unsigned long long ull;
};
int main(void) {
  printf("%d %d\n", sizeof(double), sizeof (unsigned long long));	
  union double_long_long u;
  u.dbl = -12344235235325235.3252352352938598325982359935289;
  u.ull = u.ull >> 1;
  printf("%lf\n", u.dbl);
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question