Thread: wts difference between flaot and int?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    wts difference between flaot and int?

    i writen the program for printing the number in binary format

    Code:
    #include<stdio.h>
    int main()
    {
    float number;
    printf("enter the value of number\n");
    scanf("%f",&number);
    printf("the value of number in binary format is : ");
    do
    {
    if(number & 01) 
    {
    printf("1");
    }
    else
    {
    printf("0");
    }
    number>>=1;
    }while(number !=0);
    printf("\n");
    return 0;
    }
    if i change the above program int to float or double or longdouble , i getting the error as

    4.c:10: error: invalid operands to binary &
    4.c:18: error: invalid operands to binary >>


    any one let me know why i getting this error while i changed from int to float?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Floats are real numbers, they have a decimal point whereas ints are pure integers with no decimal point. '&' and '>>' are bitwise operators that require int, char, short or long as their arguments, that's why you're getting error when you change the argumenst to float.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    why i cnat bit manuplate the float numbers ? wts reason behind this?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    Floats are real numbers,
    Yes, but be aware they are not at all the same as what we normally consider decimal numbers to be. Exact decimal numbers have fixed precision; floating point precision is slightly different, and because it is represented with bits, only values that can be created by dividing one by two are perfectly represented, (eg, 0.5, 0.25, 0.125, 0.0625) or a multiple of such (0.75, etc). Notice that DOES NOT include 0.1!

    For an illustration, compile and run this:
    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    The fact that "0.1" is not precise as a floating point number is clear in the output:

    2.600000
    2.700000
    2.799999
    2.899999


    hmmm, what happened there?

    Unfortunately, there are is no standard datatype for fixed precision numbers (such as 0.1) in C.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by crocker View Post
    any one let me know why i getting this error while i changed from int to float?
    Because bitwise arithmetic on a float makes about as much sense as "How long is purple?"
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    No, purple is a colour.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think I would argue that purple is the color of a specific light wavelength. We recognize colors with our eyes, which process photonic information. Without light, there's no purple.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    hi cpeople,

    i didt getting these colours for bit manuplations. any one can suggest me for material to understand the relation of bit manuplations with those colours(purple)?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    iMalc is basically saying that you cannot (directly) use bitwise manipulation on floating point variables. You can ignore the part of about purple if you do not understand the analogy and subsequent off-topic discussion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The point was that direct bitwise maths such as using the &-operator on a float does not make sense.
    There are certainly ways to print out the significant digits of a float in binary, and show the decimal place in the right position, but the code for that is ever so more complex than what you are using.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I found a link where it has been discussed.
    bitwise on floats
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Shifting an integer left multiplies it by powers of 2. Shifting a float doesn't. It gives you, for all intents and purposes, garbage. So, sure, you could do that with casts, but why would you want to do it? That's probably the reason why C doesn't define the operator for floats - it doesn't make much sense.

Popular pages Recent additions subscribe to a feed