c1phr
c1phr

Reputation: 590

C - Convert char to int to perform bitwise ops on output

I'm using a function (Borrowing code from: http://www.exploringbinary.com/converting-floating-point-numbers-to-binary-strings-in-c/) to convert a float into binary; stored in a char. I need to be able to perform bitwise operations on the result though, so I've been trying to find a way to take the string and convert it to an integer so that I can shift the bits around as needed. I've tried atoi() but that seems to return -1.

Thus far, I have:

char binStringRaw[FP2BIN_STRING_MAX];
float myfloat;

printf("Enter a floating point number: ");
scanf("%f", &myfloat);
int castedFloat = (*((int*)&myfloat));
fp2bin(castedFloat, binStringRaw);

Where the input is "12.125", the output of binStringRaw is "10000010100001000000000000000000". However, attempting to perform a bitwise operation on this give an error: "Invalid operands to binary expression ('char[1077]' and 'int')".

P.S. - I apologize if this is a simple question or if there are some general problems with my code. I'm very new to C programming coming from Python.

Upvotes: 3

Views: 2727

Answers (1)

Legionair
Legionair

Reputation: 423

"castedFloat already is the binary representation of the float, as the cast-operation tells it to interpret the bits of myfloat as bits of an integer instead of a float. "

EDIT: Thanks to Eric Postpischil:

Eric Postpischil in Comments: "the above is not guaranteed by the C standard. Dereferencing a converted pointer is not fully specified by the standard. A proper way to do this is to use a union: int x = (union { float f; int i; }) { myfloat } .i;. (And one must still ensure that int and float are the same size in the C implementation being used.)"

Bitwise operations are only defined for Integer-type values, such as char, int, long, ..., thats why it fails when using them on the string (char-array)

btw,

int atoi(char*)

returns the integer-value of a number written inside that string, eg.

atoi("12")

will return an integer with value 12

If you would want to convert the binary representation stored in a string, you have to set the integer bit by bit corresponding to the chars, a function to do this could look like that:

long intFromBinString(char* str){
   long ret=0;          //initialize returnvalue with zero
   int i=0;             //stores the current position in string
   while(str[i] != 0){  //in c, strings are NULL-terminated, so end of string is 0
      ret<<1;           //another bit in string, so binary shift resutl-value 
      if(str[i] == '1') //if the new bit was 1, add that by binary or at the end
         ret || 0x01;
      i++;              //increment position in string
   }
   return ret;          //return result
}

The function fp2bin needs to get a double as parameter. if you call it with castedFloat, the (now interpreted as an integer)value will be implicitly casted to float, and then pass it on.

I assume you want to get a binary representation of the float, play some bitwise ops on it, and then pass it on. In order to do that you have to cast it back to float, the reverse way you did before, so

int castedFloat = (*((int*)&myfloat));
{/*** some bitwise magic ***/}
float backcastedFloat = (*(float*)&castedFloat);
fp2bin(castedFloat, binStringRaw);

EDIT:(Thanks again, Eric):

union bothType { float f; int i; }) both;
both.f = myfloat;
{/*** some bitwise magic on both.i ***/}
fp2bin(both.f, binStringRaw);

should work

Upvotes: 2

Related Questions