-
May 17th, 2023, 04:43 PM
#1
Thread Starter
PowerPoster
on VB6 do we have '<<' and '>>' binary operators?
on C we have '<<' and '>>' binary operators... on VB6 do we have similar operators?
-
May 17th, 2023, 04:51 PM
#2
Re: on VB6 do we have '<<' and '>>' binary operators?
You can simulate them for the most part by n*2 and n\2
-
May 17th, 2023, 05:59 PM
#3
Re: on VB6 do we have '<<' and '>>' binary operators?
In VB6 no, but twinBASIC has added them.
-
May 17th, 2023, 07:37 PM
#4
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.
-
May 18th, 2023, 02:52 AM
#5
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.
-
May 18th, 2023, 03:45 AM
#6
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by OptionBase1
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:-
Is equivalent to:-
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.
-
May 18th, 2023, 07:43 AM
#7
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by Niya
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.
-
May 18th, 2023, 08:43 AM
#8
Re: on VB6 do we have '<<' and '>>' binary operators?
Yea, if he said that then I trust it to be true.
-
May 18th, 2023, 09:07 AM
#9
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by Niya
Yea, if he said that then I trust it to be true.
That's how religions are born!
-
May 18th, 2023, 09:18 AM
#10
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by VanGoghGaming
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.
-
May 18th, 2023, 09:54 AM
#11
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.
Last edited by The trick; May 18th, 2023 at 09:59 AM.
-
May 18th, 2023, 10:07 AM
#12
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?
-
May 18th, 2023, 10:09 AM
#13
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by VanGoghGaming
Why did you use a UDT for encapsulating the array?
Because it produces less code.
Originally Posted by VanGoghGaming
Are all optimizations needed or only integer overflow?
I didn't checked it. I always use all the optimizations for executables.
-
May 18th, 2023, 10:10 AM
#14
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.
-
May 18th, 2023, 11:05 AM
#15
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by The trick
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?
-
May 19th, 2023, 03:34 AM
#16
Re: on VB6 do we have '<<' and '>>' binary operators?
Originally Posted by VanGoghGaming
I don't understand, are you saying it's better to put all arrays inside UDTs? How does that work?
Fixed arrays in structures are just memory blocks. No code to initialization SAFEARRAY (the memory is in stack/section) and some other benefits.
-
May 19th, 2023, 09:14 AM
#17
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.
-
May 28th, 2023, 07:56 AM
#18
Thread Starter
PowerPoster
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|