1

I am new to c, & here I am trying to print the values stored in float & double variables in the way they are stored in memory. But compiler is not allowing me to use bitwise operators on float & double variables! I want to know why we can't use bitwise operators like '&' and '|' operators on float & double data types, & what happens if somehow we could use it? Are they any alternative methods or can we bypass such issues?

Here is the code I tried to work on,

int main()
{
 float valF = 10;
 double valD = 10;

    printf("\n\t%i\t%li\n",valI,sizeof(valF));          
 for(i = 8*sizeof(valF); i >= 0 ; i--) 
{ 
printf("%i",(valF & (1<<i))? 1 : 0);
 }

 printf("\n\t%i\t%li\n",valD,sizeof(valS)); 

for(i = 8*sizeof(valD); i >= 0 ; i--)
{
 printf("%i",(valD & (1<<i))? 1 : 0); 
}

}
  • 3
    Possible duplicate of [How to perform a bitwise operation on floating point numbers](http://stackoverflow.com/questions/1723575/how-to-perform-a-bitwise-operation-on-floating-point-numbers) – ameyCU Oct 21 '15 at 05:25
  • These are not unary operators. Unary operators are those that take only one operand like the unary minus in `y = -x` – Drunix Oct 21 '15 at 05:43
  • @ameyCU Not exactly dupe, that one is tagged for C++. – user694733 Oct 21 '15 at 11:35
  • @user694733 The answer there address both the languages . – ameyCU Oct 21 '15 at 11:36

2 Answers2

2

The binary & operator is the bitwise AND operator. It is defined only for integral types.

That is the case for all the bitwise operators.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    I guess it is only defined for intergral types because the internal bit structure of floating point numbers is in principle not determined by the language standard (in practice, it si almost always IEEE 754, right?), so te operator can not be meaningfully defined. But then, neither is that of negative integers (it could be one's or two's complement), so perhaps I'm missing something... – Nicolas Miari Oct 21 '15 at 05:52
0

C language made that way that only specific data type supports specific functionality For example in c++ you can add (+) two int variables (primitive data type) but u can't add two secondary data type (that user have made) U have to define that + for that secondary data type. Compiler made that way only provide those functionality that makes some programing sence

Atul Sharma
  • 41
  • 1
  • 6