Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Shift operations with float and double

Shift operations with float and double

Scheduled Pinned Locked Moved Java
javajsonhelp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kujtim Hyseni
    wrote on last edited by
    #1

    Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

    L D T P 5 Replies Last reply
    0
    • K Kujtim Hyseni

      Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I do not think that Java streams allow these operators, << and >> are shift operators which is why you get compile errors.

      I must get a clever new signature for 2011.

      1 Reply Last reply
      0
      • K Kujtim Hyseni

        Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Both Double and Float support the longValue() method which will return you a long primitive (if it's in the range of long). The other option for bit manipulation is to use the toHexString() method on Double or Float.

        1 Reply Last reply
        0
        • K Kujtim Hyseni

          Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          This may be a dumb question but what does bit shifting have to do with serialization? Why would you need to shift the bits in a double value in order to send it over a stream?

          L 1 Reply Last reply
          0
          • D David Skelly

            This may be a dumb question but what does bit shifting have to do with serialization? Why would you need to shift the bits in a double value in order to send it over a stream?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I think the OP is getting confused with the stream operators of C++.

            I must get a clever new signature for 2011.

            1 Reply Last reply
            0
            • K Kujtim Hyseni

              Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              see this: Bit Operations Tutorial @ oracle.com[^] regards Torsten

              I never finish anyth...

              1 Reply Last reply
              0
              • K Kujtim Hyseni

                Hello, I need to know hot to use shift operators << and >> in Java with float and double types. I tried to use them but I got compile error. I needed it for serialization data e.g. sending over the streams.

                P Offline
                P Offline
                parths
                wrote on last edited by
                #7

                I'm assuming that you want to use the bit-representation of the floating point value with the shift operators to create a float from a byte-stream, and vice-versa The java Float class (here[^]) contains functions: floatToIntBits, floatToRawIntBits -- Convert from a float to an integer containing the float value's bit representation intBitsToFloat: Converts an integer value (containing a float value's bit representation) to a Float variable. So basically you can do something like:

                byte[] data = ...;
                int rdInt = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); // Or the other way round depending on endian-ness.
                float rdFloat = Float.intBitsToFloat(rdInt);

                ... And do the opposite to convert back from float to bytes.

                "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman "Philosophy is a study that lets us be unhappy more intelligently." -Anon.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups