Results 1 to 18 of 18

Thread: on VB6 do we have '<<' and '>>' binary operators?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    on VB6 do we have '<<' and '>>' binary operators?

    on C we have '<<' and '>>' binary operators... on VB6 do we have similar operators?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: on VB6 do we have '<<' and '>>' binary operators?

    You can simulate them for the most part by n*2 and n\2

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: on VB6 do we have '<<' and '>>' binary operators?

    In VB6 no, but twinBASIC has added them.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: on VB6 do we have '<<' and '>>' binary operators?

    There's some good stuff for shifting bits on the VBSpeed website.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Gosh, that site is useful. Some handy functions there. If only I hadn't written my own already. That would have been very useful a couple of years ago.

    Do we have a list of useful (but ever-diminishing) resources and links anywhere?
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Quote Originally Posted by OptionBase1 View Post
    You can simulate them for the most part by n*2 and n\2
    Actually it's multiplication and division by powers of 2. Eg:-
    Code:
    x * (2 ^ 10)
    Is equivalent to:-
    Code:
    x << 10
    Two things to note though. One, unless the VB6 compiler can recognize this pattern and convert it to a bit shift in native code it would be much slower than having a real bit shift operator in the language. The second thing is that this is vulnerable to overflow errors:-
    Code:
        Dim i As Integer
        Dim result As Integer
        
        i = 1
        
        'This overflows
        result = i * (2 ^ 15)
    
    If we did the same thing in a language with actual bit shift operators, we see that it works. VB.Net for example has them:-
    Code:
            Dim s As Short = 1
            Dim result As Short
            Dim result2 As Short
    
            'Result is -32768
            result = s << 15
    
            'This overflows
            result2 = s * (2 ^ 15)
    
    So be very careful when converting code from languages with actual bit shift operators to VB6 where you'd have to use divisions and multiplications to simulate them.

    There are other concerns as well for example what happens when a bit "falls off the edge" so to speak. Does it get discarded? Does it wrap around? For example in VB.Net it will wrap around:-
    Code:
            Dim s As Short = 1
            Dim result As Short
    
            'Result is 2 because the bit been wraped around
            result = s << 17
    
    A Short is 16 bits wide so when we shift the first bit 16 places to the left, it falls outside so it wraps it around by rotating it back into the first position. We shifted by 17 which means we have to shift by 1 after rotating, this is why the value is 2.

    Division and multiplication will not behave like this when used to simulate bit shifting. It will either overflow or give inaccurate results so be aware of these nuances.
    Last edited by Niya; May 18th, 2023 at 04:05 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Quote Originally Posted by Niya View Post
    unless the VB6 compiler can recognize this pattern and convert it to a bit shift in native code it would be much slower than having a real bit shift operator
    According to The Trick, if the base is 2 and the exponent is a constant (and we have integer overflow checking turned off), the compiler is smart enough to compile as a bit-shift. I haven't personally checked that, but The Trick definitely knows his stuff.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Yea, if he said that then I trust it to be true.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,324

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Quote Originally Posted by Niya View Post
    Yea, if he said that then I trust it to be true.
    That's how religions are born!

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Quote Originally Posted by VanGoghGaming View Post
    That's how religions are born!
    Having read quite a bit of epistemological philosophy, that's actually "authority", not "religion". I'm not sure The Trick said that God whispered in his ear and told him that.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Code:
    Option Explicit
    
    Type x
        l(30) As Long
    End Type
    
    Sub Main()
        Dim l As x
        Dim i As Long
        
        i = Get1
        
        l.l(0) = i * &H2
        l.l(1) = i * &H4
        l.l(2) = i * &H8
        l.l(3) = i * &H10
        l.l(4) = i * &H20
        l.l(5) = i * &H40
        l.l(6) = i * &H80
        l.l(7) = i * &H100
        l.l(8) = i * &H200
        l.l(9) = i * &H400
        l.l(10) = i * &H800
        l.l(11) = i * &H1000
        l.l(12) = i * &H2000
        l.l(13) = i * &H4000
        l.l(14) = i * &H8000
        l.l(15) = i * &H10000
        l.l(16) = i * &H20000
        l.l(17) = i * &H40000
        l.l(18) = i * &H80000
        l.l(19) = i * &H100000
        l.l(20) = i * &H200000
        l.l(21) = i * &H400000
        l.l(22) = i * &H800000
        l.l(23) = i * &H1000000
        l.l(24) = i * &H2000000
        l.l(25) = i * &H4000000
        l.l(26) = i * &H8000000
        l.l(27) = i * &H10000000
        l.l(28) = i * &H20000000
        l.l(29) = i * &H40000000
        l.l(30) = i * &H80000000
    
        foo l
        
    End Sub
    
    ' // avoid constant optimization
    Public Function Get1() As Long
        Get1 = 1
    End Function
    
    ' // avoid unused arr removing
    Public Sub foo(l As x)
    
    End Sub
    Compile it with all the optimizations:



    BTW, i forgot to add & prefix to &H8000 constant and NEG instruction was there because of that.

  12. #12
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,324

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Why did you use a UDT for encapsulating the array?

    Are all optimizations needed or only integer overflow?

  13. #13

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Ahhh, my mistake. It's the power-of-2 multiplication that's converted to bit-shifting, not the exponent operator. You actually hinted at that before, and I didn't understand your meaning.

    Trick, sorry for the mis-interpretation.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,324

    Question Re: on VB6 do we have '<<' and '>>' binary operators?

    Quote Originally Posted by The trick View Post
    Because it produces less code.
    I don't understand, are you saying it's better to put all arrays inside UDTs? How does that work?

  16. #16

  17. #17
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,324

    Re: on VB6 do we have '<<' and '>>' binary operators?

    Yeah it makes sense when you put it that way. I always wondered why UDTs with fixed array members worked fine with API calls.

  18. #18

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: on VB6 do we have '<<' and '>>' binary operators?

    i hate when i didn't get notifications when they are activated
    or it's a hotmail problems
    thanks for to all
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width