Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int float_bit_return(int num, int loc)
{
    return (num & 1 << loc) ? 1 : 0;
}


What I have tried:

i am trying to print bits of a float value using bitwise operators
Posted
Updated 4-Jun-21 2:41am

<< is the "shift left" operator:
x << y
returns x shifted y bits to the left.

This is most obvious in binary, but this may help you understand. If x is 1, then:
y      Binary  Decimal
0    00000001        1
1    00000010        2
2    00000100        4
3    00001000        8
4    00010000       16
5    00100000       32
6    01000000       64
7    10000000      128
As y rises, the number of bit places x is moved.

& is the Binary AND operator, and is used to mask out all zero bits from the input - because only bits which have a one on each side produce 1's in the output.

So ... your code shifts 1 left by loc places, and returns 1 if that bit in the input num was set.
 
Share this answer
 
The function returns the value of the loc bit of the integer variable num. Note you could rewrite it this way
C
int float_bit_return(int num, int loc)
{
    return ((num >> loc) & 1);
}

(the inner braces are not needed due to C operator precedence rules, but I find the code more readable with them).

You may actually use use such a function to inspect the bits of a float value, as the following code shows (it assumes sizeof(float)==sizeof(int))
C
#include <stdio.h>
int float_bit_return(int num, int loc)
{
    return ((num >> loc) & 1);
}

int main()
{
  int i = 5;
  float f = 3.25f;

  printf("bits of the integer value %d\n", i);
  for (int n=0; n<sizeof(int)*8; ++n)
    printf("%02d %d\n", n, float_bit_return(i, n));


  printf("bits of the float value  %f\n", f);
  for (int n=0; n<sizeof(int)*8; ++n)
    printf("%02d %d\n", n, float_bit_return(*(int*)&f, n));

  return 0;
}


Output:
[...]
bits of the float value  3.250000
00 0
01 0
02 0
03 0
04 0
05 0
06 0
07 0
08 0
09 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 1
21 0
22 1
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 1
31 0

with
sign: bit 31 equal to zero -> positive number
exponent bits 30-23 = 128 -> 2^1
mantissa bits 22-0 = .101b -> 1.101b

so,
f = 1.101b * 2^1 = 11.01b = 3.25

[update]
The same result could be obtained using a (possibly more intuite) union:
C
#include <stdio.h>
int float_bit_return(int num, int loc)
{
    return ((num >> loc) & 1);
}

int main()
{
  union // the int i and the float f share the same memory area (hence the same bit pattern)
  {
    int i;
    float f;
  } u;

  u.f = 3.25;

  printf("bits of the float value  %f\n", u.f);
  for (int n=0; n<sizeof(int)*8; ++n)
    printf("%02d %d\n", n, float_bit_return( u.i, n ));

  return 0;
}

[/update]
 
Share this answer
 
v4
Comments
KarstenK 4-Jun-21 8:59am    
You are absolutly right about commenting the usage of braces.
CPallini 4-Jun-21 9:07am    
Thank you.
Rahul Dicholkar 4-Jun-21 11:26am    
float_bit_return(*(int*)&f, n));
why there is need to pass the address of f and what is the importance of typecasting ?
Rahul Dicholkar 4-Jun-21 11:26am    
float_bit_return(*(int*)&f, n));
why there is need to pass the address of f and what is the importance of typecasting ?
CPallini 4-Jun-21 12:18pm    
That's necessary, we need to pass f as an integer without altering its bit pattern.
(int)f
for instance, wouldn't work.

The expression, roughly speaking means: take the address of the float variable, and handle its content as an integer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900