How to prove float is of 32 bit?

This is a discussion on How to prove float is of 32 bit? within the C Programming forums, part of the General Programming Boards category; How to prove using C program that float is of 32 bit?...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    How to prove float is of 32 bit?

    How to prove using C program that float is of 32 bit?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    printf("float size is %u bits\n", sizeof(float) * CHAR_BIT);
    Last edited by bithub; 08-02-2009 at 01:02 AM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    float.h
    And associated constants.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Thanks but i need to write the program using basic shift, logical and other operators...Is it possible?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,264
    To answer your question literally: yes, it is possible.

    Beyond that, I will say no more: your problem has all the hallmarks of a homework exercise, so you should look here.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by anirban View Post
    Thanks but i need to write the program using basic shift, logical and other operators...Is it possible?
    Nota bene: you cannot use shift or bitwise operators on a float variable.

  7. #7
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Yes that's true just discovered. So any other way plz?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    Quote Originally Posted by anirban View Post
    Thanks but i need to write the program using basic shift, logical and other operators...Is it possible?
    Utter nonsense.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Sebastiani is right. Binary operators are not portably supported for floats (binary in terms of shift/and, not like + or &&) and very complicated - I really doubt you need them. Logicals & other operators are well supported (except %, you'll need fmod)

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,264
    It is not necessary to use bitwise operations on float variables for this. The only information needed is the size of the type and the number of bits in a variable of that size. The relationship between sizeof(type) and number of bits in a type is fixed for any given implementation.

    sizeof() is an operator with pretty well-defined semantics, after all, and bitwise operations are readily applicable to char types.

    The only exception would be processor and memory architectures that do not involve bits. But such architectures are pretty rare practically, and the question is meaningless on them anyway.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    And, for full portability, also check CHAR_BIT.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 02:41 PM
  2. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM
  3. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM
  4. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM
  5. help w/another program!!!
    By edshaft in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21