none
Signed and Unsigned variables in VB.NET RRS feed

  • Question

  • Hi,

    I need to do a conversion by shifting bits. As you see this below code

            Dim x As Integer = (&H2 << 8) Or &HCC
            Me.Text = x

    It shows me 716 which is correct. However for this conversion:

            Dim x As Integer = (&HFF << 8) Or &HFC
            Me.Text = x

    Instead of -4 it shows me 65532 which is in correct. However, in numerical system in VB.NET is completely correct because it is not related to first negative bit. for Solving this problem what do you suggest?

    Wednesday, April 11, 2018 5:56 AM

All replies

  • I think that you should use Short:

    Dim x As Short = (&HFFS << 8) Or &HFC

    Wednesday, April 11, 2018 6:17 AM
  • Thank you Viorel_ very much

    it works fine.



    • Edited by Parisa_1 Wednesday, April 11, 2018 6:51 AM
    Wednesday, April 11, 2018 6:49 AM
  • Hi Parisa,

    An integer is the way, the shortest memory length of bytes can set in the accumulator. 

    Currently it is in VB not even real representing that, but is to keep in line with former VB kept to 4 bytes. 

    In VB it is then set to 8 bytes. 

    No problem because VB is made to solve real life solutions, not for bit f.....g in the way we where using the computer in 1946. 

    Therefore you would use program languages as C or Intel Assembler or more hibernate C++. 

    https://en.wikipedia.org/wiki/Accumulator_(computing)


    Success
    Cor




    Wednesday, April 11, 2018 6:51 AM
  • Hi,

    I need to do a conversion by shifting bits. As you see this below code

            Dim x As Integer = (&H2 << 8) Or &HCC
            Me.Text = x

    It shows me 716 which is correct. However for this conversion:

            Dim x As Integer = (&HFF << 8) Or &HFC
            Me.Text = x

    Instead of -4 it shows me 65532 which is in correct. However, in numerical system in VB.NET is completely correct because it is not related to first negative bit. for Solving this problem what do you suggest?

    So integer works so long as your assignment statement is correct.  Integer &HFF is not -1, it is 255.  The result you received in the second example is correct.

    If you are going to use hex literals then they must match the data type selected.  e.g. If you want an integer to hold the hex representation of -1 then the correct literal is &HFFFFFFFF.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Wednesday, April 11, 2018 3:05 PM
  • Thank you Viorel_ very much

    it works fine.




    The question remains unanswered. Did you get an answer?

    La vida loca

    Thursday, April 12, 2018 11:04 PM
  • Hi Parisa_1,

    If you have solved your issue, please remember to close your thread by marking the helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 19, 2018 9:23 AM
    Moderator