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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. byte to int -> bit shifting confusion [modified]

byte to int -> bit shifting confusion [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++javadata-structuresquestion
5 Posts 3 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.
  • S Offline
    S Offline
    Souldrift
    wrote on last edited by
    #1

    Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):

    unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
    ((m_aHeader[5] & 0xff) << 16) |
    ((m_aHeader[6] & 0xff) << 8) |
    (m_aHeader[7] & 0xff);

    Now why is this different than

    unsigned int time = ((m_aHeader[4] ) << 24) |
    ((m_aHeader[5] ) << 16) |
    ((m_aHeader[6] ) << 8) |
    (m_aHeader[7] );

    m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with

    m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
    m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
    m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
    m_aHeader[7] = (BYTE) (time & 0xff);

    If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,

    long time = ((m_aHeader[4] ) << 24) |
    ((m_aHeader[5] ) << 16) |
    ((m_aHeader[6] ) << 8) |
    (m_aHeader[7] );

    modified on Wednesday, August 26, 2009 3:44 AM

    C S 2 Replies Last reply
    0
    • S Souldrift

      Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):

      unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
      ((m_aHeader[5] & 0xff) << 16) |
      ((m_aHeader[6] & 0xff) << 8) |
      (m_aHeader[7] & 0xff);

      Now why is this different than

      unsigned int time = ((m_aHeader[4] ) << 24) |
      ((m_aHeader[5] ) << 16) |
      ((m_aHeader[6] ) << 8) |
      (m_aHeader[7] );

      m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with

      m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
      m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
      m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
      m_aHeader[7] = (BYTE) (time & 0xff);

      If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,

      long time = ((m_aHeader[4] ) << 24) |
      ((m_aHeader[5] ) << 16) |
      ((m_aHeader[6] ) << 8) |
      (m_aHeader[7] );

      modified on Wednesday, August 26, 2009 3:44 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Provided m_aHeader[k] (k=4..7) is a byte, there is no difference, i.e. you may write ( note there is NO 0xff close to m_aHeader[7])

      unsigned int time = (m_aHeader[4] << 24) | (m_aHeader[5] << 16) | (m_aHeader[6] << 8) | m_aHeader[7];

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S 1 Reply Last reply
      0
      • C CPallini

        Provided m_aHeader[k] (k=4..7) is a byte, there is no difference, i.e. you may write ( note there is NO 0xff close to m_aHeader[7])

        unsigned int time = (m_aHeader[4] << 24) | (m_aHeader[5] << 16) | (m_aHeader[6] << 8) | m_aHeader[7];

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        Souldrift
        wrote on last edited by
        #3

        I know, thanks. the m_aHeader[7] 0xff was a typo. and yes, there should be no difference. I just updated my post, since I found the difference is only in java :(.

        1 Reply Last reply
        0
        • S Souldrift

          Hi there, I got a little confused here, maybe someone can help with this noob question. Following code piece to convert byte to int (supposedly big endian):

          unsigned int time = ((m_aHeader[4] & 0xff) << 24) |
          ((m_aHeader[5] & 0xff) << 16) |
          ((m_aHeader[6] & 0xff) << 8) |
          (m_aHeader[7] & 0xff);

          Now why is this different than

          unsigned int time = ((m_aHeader[4] ) << 24) |
          ((m_aHeader[5] ) << 16) |
          ((m_aHeader[6] ) << 8) |
          (m_aHeader[7] );

          m_aHeader is a byte array. Edit: Just found, my problem only occurs when I use the first piece of code in java (in c++ it works fine). I create the bytes in c++ with

          m_aHeader[4] = (BYTE) ((time >> 24) & 0xff);
          m_aHeader[5] = (BYTE) ((time >> 16) & 0xff);
          m_aHeader[6] = (BYTE) ((time >> 8) & 0xff);
          m_aHeader[7] = (BYTE) (time & 0xff);

          If I read it in c++ with the further above mentioned code pieces, everything is fine. If I try this in java, though,

          long time = ((m_aHeader[4] ) << 24) |
          ((m_aHeader[5] ) << 16) |
          ((m_aHeader[6] ) << 8) |
          (m_aHeader[7] );

          modified on Wednesday, August 26, 2009 3:44 AM

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          The reason you generally want to and with 0xff is to ensure that when your byte is promoted to int (as it will be internally in the expression), it is zero-extended, i.e. the top 24 bits of the int are set to zero, not one. Why would they be set to one? Basically if the language sign-extends the byte when it makes it an int AND the top bit of the byte is set. Now, if you use 'unsigned char' for BYTE and 'unsigned int', you should be OK. If you used 'signed char' for BYTE, then you could have issues.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          S 1 Reply Last reply
          0
          • S Stuart Dootson

            The reason you generally want to and with 0xff is to ensure that when your byte is promoted to int (as it will be internally in the expression), it is zero-extended, i.e. the top 24 bits of the int are set to zero, not one. Why would they be set to one? Basically if the language sign-extends the byte when it makes it an int AND the top bit of the byte is set. Now, if you use 'unsigned char' for BYTE and 'unsigned int', you should be OK. If you used 'signed char' for BYTE, then you could have issues.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            S Offline
            S Offline
            Souldrift
            wrote on last edited by
            #5

            Yes, that´s it. Thank´s a lot. Just found out that it was a signed/unsigned issue. Didn´t know, though, why java would make a difference between

            int time = data[3];

            result is -16 and

            int time = data[3] & 0xff;

            result is 240 (data[3] being a byte holding 11110000) So thanks again. I love this forum :). Souldrift

            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