Results 1 to 33 of 33

Thread: [RESOLVED] whether boolean has a symbol ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    236

    Resolved [RESOLVED] whether boolean has a symbol ?

    hi

    double = #
    long = &
    single = !
    string = $

    whether boolean has a symbol ???

  2. #2
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    theres none,
    there 2 more: integer %, decimal @

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

    Re: whether boolean has a symbol ?

    @ is currency, not decimal.

    Decimals can only be represented in Variants, and don't have one of those symbols.

    And there are a few others that don't have those symbols, like Date, and LongLong in a Variant. Also, Variants themselves don't have a particular symbol like that. However, if you forget to declare the type, you'll get a Variant.
    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.

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

    Re: whether boolean has a symbol ?

    And, just for some completeness, Booleans are just a special case of an Integer, much the same way that a Date is just a special case of the Double.

    Typically, a Boolean only takes on the values of 0 or -1. But, with some trickery, you can make them take on any value that an Integer can have.
    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
    Join Date
    Feb 2006
    Posts
    24,482

    Re: whether boolean has a symbol ?

    Quote Originally Posted by Elroy View Post
    And, just for some completeness, Booleans are just a special case of an Integer, much the same way that a Date is just a special case of the Double.
    It isn't really as simple as that and it isn't a good way to look at it. It probably feels clever right up until the first time such assumptions bite you hard and you waste untold hours trying to unravel what somebody did trying to be clever. If you want type conversions always specify them explicitly.


    In any case you should avoid using those type suffix symbols except where you need them: some decimal and hex numeric literal values.

    Using them to define variables and functions just creates a cluttered mess that is harder to read and thus produces more errors. It is the opposite of good practice. They are a throwback that probably would have been deleted if we ever got a VB7.

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

    Re: whether boolean has a symbol ?

    Quote Originally Posted by dilettante View Post
    It isn't really as simple as that and it isn't a good way to look at it.
    I don't disagree with that at all. However, it is useful for me to know that a Boolean is two bytes (16 bits). (Maybe think about it sitting in a UDT.) Also, it's useful for me to know that a TRUE=1111111111111111 (or -1), and a FALSE=0000000000000000 (or 0). And that this is quite different from C and most other languages (especially for the TRUE).

    But, you are correct. It would be really bad programming practice if a VB6 Boolean were used for anything more than a True/False Boolean. It's not good practice to do arithmetic with these things, as is often done in C (and related languages).

    And yeah, I personally NEVER use the character variable typing suffixes. If VB6.5 ever comes about, I doubt they'll remove them, just to preserve backward compatibility, but I agree that we should avoid them.
    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.

  7. #7
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    so, it is better to use a byte instead of a boolean, like

    if myvar (byte) = 0 then for false and = 1 for true instead of myvar (boolean) = true and = false?
    as byte is 1 byte and boolean 2 bytes?
    and what about speed, if we are gonna check 1M variables if true or false.
    if boolean is slower and also uses 2 bytes, its quite useless.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: whether boolean has a symbol ?

    Well, it probably won't make a lot of difference, but since we work with 32-bit and 64-bit processors, in general accessing a 32-bit value is faster than accessing a 16-bit value, and accessing an 8-bit value is slower as well. It has to do with memory alignment when reading into a register. A 32-bit value will read directly into a 32-bit portion of the register in one access, where a 16-bit or 8-bit value may not align with the natural boundary of the memory bus and register so involves extra hardware cycles to align and mask the bits appropriately into the register, so is slower probably 75% of the time.
    So, is it more important to save a little bit of speed or a 50% of your memory, its up to you.

    I've seen cases with chess tables and other options, where it made since to only use 1-bit booleans, so you could have 16 booleans in the space that VB would normally take, but accessing those values would take a little longer, but not that much if you use a look up table to isolate the bit and a boolean operation to test it. In some situations it could actually be faster as you could test a group of 16, 32 or 64 bit boolean values in one shot to see if any in the group were true or false.

    Another example is where it could speed up things is where you might have 64-booleans store in a 64-bit value that represent the state of 64 things and you only want to do something if one or more of them changed states. You could XOR the current value with the previous history, and if the result is 0, then none of the 64 values have changed so you saved having to test 64 things independently.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: whether boolean has a symbol ?

    Quote Originally Posted by dilettante View Post
    They are a throwback that probably would have been deleted if we ever got a VB7.
    They were supported in VB7, i.e. VB.NET 2002, and they are still supported in VB 2017, so it seems rather unlikely that they would have been removed if a COM-based successor to VB6 was ever created.

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: whether boolean has a symbol ?

    they aren't going anywhere. They can be used on literals. It's one of the few ways to ensure you don't get a cast error when trying to assign 1 to a decimal. They also serve the purpose of letting the code reader know in-line what data type the variable is.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,163

    Re: whether boolean has a symbol ?

    Quote Originally Posted by dilettante View Post
    Using them to define variables and functions just creates a cluttered mess that is harder to read and thus produces more errors. It is the opposite of good practice. They are a throwback that probably would have been deleted if we ever got a VB7.
    I doubt Olaf would agree to remove these monstrosities from his vNext. . . as far as I've seen snippets of his.

    cheers,
    </wqw>

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: whether boolean has a symbol ?

    Since i never used any of those symbols in my life i'm completely indifferent to them.
    Use proper hungarian notation and be done with it.
    Code:
    Dim sTemp As String
    Dim iTemp As Integer
    Dim lTemp As Long
    Dim bTemp As Boolean
    Dim dTemp As Double
    Dim snTemp As Single
    Dim byTemp As Byte
    Dim curTemp As Currency
    'and so on....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  13. #13
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,057

    Re: whether boolean has a symbol ?

    Quote Originally Posted by Zvoni View Post
    Since i never used any of those symbols in my life i'm completely indifferent to them.
    Use proper hungarian notation and be done with it.
    Code:
    Dim sTemp As String
    Dim iTemp As Integer
    Dim lTemp As Long
    Dim bTemp As Boolean
    Dim dTemp As Double
    Dim snTemp As Single
    Dim byTemp As Byte
    Dim curTemp As Currency
    'and so on....
    Exactly. Avoid using those symbols if at all possible.

  14. #14

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: whether boolean has a symbol ?

    Quote Originally Posted by Elroy View Post
    I don't disagree with that at all. However, it is useful for me to know that a Boolean is two bytes (16 bits). (Maybe think about it sitting in a UDT.) Also, it's useful for me to know that a TRUE=1111111111111111 (or -1), and a FALSE=0000000000000000 (or 0). And that this is quite different from C and most other languages (especially for the TRUE).

    But, you are correct. It would be really bad programming practice if a VB6 Boolean were used for anything more than a True/False Boolean. It's not good practice to do arithmetic with these things, as is often done in C (and related languages).

    And yeah, I personally NEVER use the character variable typing suffixes. If VB6.5 ever comes about, I doubt they'll remove them, just to preserve backward compatibility, but I agree that we should avoid them.
    It was my understanding that FALSE was defined as 0, and TRUE was defined as NOT FALSE .... which is what allows things like this possible:
    Code:
    Dim x as Integer
    x = 4
    if x then
      MsgBox("This is True")
    End If
    As long as the expression evaluates to a TRUE (or NOT FALSE) state the message box will get displayed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,163

    Re: whether boolean has a symbol ?

    Quote Originally Posted by techgnome View Post
    As long as the expression evaluates to a TRUE (or NOT FALSE) state the message box will get displayed.
    Yes, but still x is not equal to True
    thinBasic Code:
    1. '
    2.     If x = True Then
    3.       MsgBox "Unreachable"
    4.     End If
    Only when you assign -1 to x you can get x = True to succeed.

    You can apply the same reasoning for Boolean variables, provided that you somehow coerce them to contain anything besides 0 and -1 e.g. a 4 like in your example can be done with this:
    thinBasic Code:
    1. '
    2.     Dim x As Boolean
    3.     Call CopyMemory(x, 4, 4)
    4.     If x Then
    5.       MsgBox ("This is True")
    6.     End If
    7.     If x = True Then
    8.       MsgBox "Unreachable"  '<--- does not get called
    9.     End If
    In the snippet above the neither x = False nor x = True conditions succeed -- just FYI.

    cheers,
    </wqw>

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

    Re: whether boolean has a symbol ?

    Goodness,

    There are a couple of points made about these Booleans in the above. For grins, I think I'll try and address them. Here's what I see as the points:

    • How do "If" statements work in terms of making boolean decisions?
    • Are Booleans really a special case of Integers, and how can we get into trouble with them?
    • Which is faster to use as a boolean type variable, Boolean, Byte, or maybe Long?

    I think I'll address the last point in a subsequent post.

    So, how do "If" statements work? For me, it's a bit more involved than that. First, I think it's important to realize that, with respect to boolean type operations, there are two kinds of operators, bitwise and logical. (By "logical", I mean we're trying to make a Yes/No, True/False decision.)

    And, I always contrast VB6 to C to keep this straight. In C, for our bitwise operators, we've got &, |, ^, ~. And then, for our logical operators, we've got <, >, <=, >=, ==, !, !=, &&, ||.

    VB6 makes a bit of a mess with this, and makes it much more difficult to recognize when you're doing a logical operation versus a bitwise operation. However, we can safely say the following: For VB6, AND, OR, NOT, XOR, IMP, EQV are our bitwise operators. And = (when not being used for assignment), <, >, <=, >=, <> are our logical operators. And, to make matters worse, in the VB6 MSDN, the AND, OR, NOT, XOR, IMP, EQV are called "Logical Operators" which is absolutely wrong. If anyone likes, I can easily prove that these are bitwise and not purely logical.

    Now, just to be clear, in C, a bitwise operator is typically thought of as an operator that operates on each bit independently. The simplest is the ~ (or NOT in VB6). This operator just takes each bit and flips it. Now, a logical operator is one that always reduces to a TRUE or FALSE. In C, that would be an integer 1 or 0. In VB6, that would be an integer -1 or 0. (The 1 vs -1 is another issue, but I'll let that go for now.)

    Now, another important thing to remember is that VB6 (and, any language, for that matter) is going to often create temporary variables (that aren't represented in the source code) to get its work done. And, we're better off thinking of "If" statements in these terms. For instance, we'll never get into trouble if we think of the decision expression in an "If" statement as always reducing to a temporary Boolean before the decision is made. For instance, in the above posts, Techgnome presented the statement "If x Then". However, we could also equivalently say "If x <> 0 Then", and that's actually being done in Technome's version. Or, here's yet another way to accomplish the same thing:

    Code:
    
        Dim x As Integer
        Dim b As Boolean
    
        x = 4
        b = x <> 0
        If b Then
            '...
    

    All I've done is explicitly create a boolean for testing whereas, in Technome's example, it was implicitly created. However, I believe it's safe to say that an "If" statement is always reduced to a TRUE or FALSE Boolean before the decision is made.

    Now, how can not thinking of AND, OR, NOT, XOR, IMP, EQV as bitwise get us into trouble? Still using Technome's example, the following is an excellent illustration:

    Code:
    
        Dim x As Integer
    
        x = 4
        x = Not x
        If x Then   ' Still evaluates to TRUE.
            '...
    

    Try it if you don't believe me.

    And then, for using a true logical operator in VB6, we could do:

    Code:
    
        Dim x As Integer
        Dim b As Boolean
    
        x = 4
        b = x <> 5
        If b Then   ' Evaluates to FALSE.
            '...
    

    Or, we could shorten it to:

    Code:
    
        x = 4
        If x <> 5 Then   ' Evaluates to FALSE.
            '...
    

    In that last example, we can appreciate that an implicit temporary variable is being created before the decision is made.

    So, the take-home message is that it's important to appreciate the differences in bitwise versus logical operators. Now, how might we get into trouble trying to use Booleans as Integers. Here's a novel piece of code (wrapped into a Form_Load for you to easily examine):

    Code:
    
    Option Explicit
    '
    Private Declare Function GetMem2 Lib "msvbvm60.dll" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
    '
    
    Private Sub Form_Load()
        Dim i As Integer
        Dim j As Integer
        Dim b As Boolean
    
    
        i = 5
        GetMem2 i, b
    
        If b Then Debug.Print "We're TRUE"  ' <--- reports as TRUE.
    
        b = Not b ' <-------- Think about what this is actually doing, and how many times you've used a NOT with a Boolean !!!!!
    
        If b Then Debug.Print "We're TRUE"  ' <--- still reports as TRUE!!!!!
        ' How can the above be?  It's a Boolean!!!!
    
        GetMem2 b, j
        Debug.Print j   ' <--- reports -6, which is a bit-flipped 5.
    
    End Sub
    
    

    This illustrates the true bitwise nature of the NOT operator as well as how treating Booleans as Integers can cause confusion.

    Okay, I'll set up some timing tests to answer that point #3.

    Take Care,
    Elroy
    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.

  18. #18
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: whether boolean has a symbol ?

    Hungarian notation is a little dated... but then again, this is VB6.

    https://en.wikipedia.org/wiki/Hungar...table_opinions

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

    Re: whether boolean has a symbol ?

    This post is about the speed of using various variable types for a boolean. I thought I'd test this with the simple NOT bitwise operator, as the discussion above is mostly about Boolean and making Yes/No decisions.

    First, I wasn't sure if VB6 was doing some implicit typecasting when using the NOT operator, so I did the following:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim bb As Byte
        Dim b As Boolean
        Dim i As Integer
        Dim l As Long
    
    
        Debug.Print TypeName(Not bb)
        Debug.Print TypeName(Not b)
        Debug.Print TypeName(Not i)
        Debug.Print TypeName(Not l)
    
    
    End Sub
    

    Happily, it reported...

    Code:
    Byte
    Boolean
    Integer
    Long

    So, hopefully, it's doing the NOT with the variable type being supplied, and not doing any implicit typecasting.

    Next, I pulled in my high-precision timer class (TimerEx) and wrote the following code:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim bb As Byte
        Dim b As Boolean
        Dim i As Integer
        Dim l As Long
    
        Dim dStart As Double
        Dim dDelta As Double
        Const cnt As Long = 100000000
        Dim idx As Long
    
        Debug.Print "Instantiate: "; TimerEx.Value   ' Just to get it instantiated.
    
    
        dStart = TimerEx.Value
        For idx = 1& To cnt
            bb = Not bb             ' Flip bits on a Byte type.
        Next
        dDelta = TimerEx.Value - dStart
        Debug.Print "   Byte: "; dDelta
    
    
        dStart = TimerEx.Value
        For idx = 1& To cnt
            b = Not b               ' Flip bits on a Boolean type.
        Next
        dDelta = TimerEx.Value - dStart
        Debug.Print "Boolean: "; dDelta
    
    
        dStart = TimerEx.Value
        For idx = 1& To cnt
            i = Not i               ' Flip bits on an Integer type.
        Next
        dDelta = TimerEx.Value - dStart
        Debug.Print "Integer: "; dDelta
    
    
        dStart = TimerEx.Value
        For idx = 1& To cnt
            l = Not l               ' Flip bits on a Long type.
        Next
        dDelta = TimerEx.Value - dStart
        Debug.Print "   Long: "; dDelta
    
    
    
    End Sub
    

    Interestingly, Boolean, Integer, and Long all ran neck-and-neck, with Byte being half the speed. I ran it several times, but here's a typical output of my test:

    Code:
    
    Instantiate:  263585.845324075
       Byte:  1.04262877959991
    Boolean:  0.558883882767987
    Integer:  0.557122148922645
       Long:  0.573386551230215
    

    Just to make sure the code-order wasn't somehow making a difference, I put the Byte test at the bottom, but the results were approximately the same. Therefore, there's certainly no advantage to using Byte variables as Booleans. And, there doesn't seem to be any advantage to trying to match our register sizes either.

    Then, it dawned on me that running as machine code (rather than p-code) might make a difference, so I compiled it. Compiled, here are the results I got (two runs):

    Code:
    Instantiate:  264123.701080343 
       Byte:  0.0437085754820146
    Boolean:  0.0444080176530406 
    Integer:  0.151188194809947 
       Long:  0.149570871435571 
       
    Instantiate:  264127.387196706 
       Byte:  0.0439036753377877
    Boolean:  0.0428782156668603
    Integer:  0.147472180018667 
       Long:  0.148384226427879

    So, the Byte type definitely ran better compiled, and Integer and Long depreciated with respect to Boolean. Therefore, when considering either compiled or p-code, it seems that Boolean is our best choice.

    I've also attached the entire project for anyone interested. The Project1 is for running in the IDE, and Project2 is for compiling (writing results to a file). You'll need to patch up your file's path if you play around with the compiled version. Both projects share the TimerEx class.

    Best Regards,
    Elroy
    Attached Files Attached Files
    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.

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

    Re: whether boolean has a symbol ?

    Ohhh, I love Hungarian and CamelCase notation.

    I even use it when coding in C, although I'm definitely an outcast using Hungarian and CamelCase in that community. But it just seems ideal to me, especially for any strongly typed language such as VB6 or C.

    Now, I often do coding in MatLab and PHP, both loosely typed languages. PHP is particularly loosely typed. It's as if you're coding in VB6, but the "Option Explicit" statement has been taken away from you, all "Dim" statements have been removed, and every variable is always declared as a Variant. It's near impossible to just look at a variable and determine what type it is, and it can easily change types with a trivial operation.

    Personally, I highly prefer strongly typed languages. It's just one less thing I must keep track of when writing code, or coming back to code I haven't looked at in a while.

    Just My Humble Opinion,
    Elroy
    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.

  21. #21
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    thanks for testing it.
    compiled is always the choice of testing speed, i have encountered this before that IDE <> Compiled is so different we can't even compare it.
    so, conclusion is that byte/boolean are the almost identical in speed, the difference is very little, but if we do have thousands of variables, byte is better to use for true/false as it will also use less memory.
    also, with byte, we could have "0=undefined,1=false,2=true", to mimic the undefined properties some languages have.
    too bad we dont have signed byte, -128 to 127, something i would request in VB7.
    Last edited by baka; Oct 19th, 2018 at 10:15 AM.

  22. #22
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: whether boolean has a symbol ?

    Quote Originally Posted by baka View Post
    ...but if we do have thousands of variables, byte is better to use for true/false as it will also use less memory.
    Yeah... no... that's not the take away at all... a byte isn't going to take up "less" memory... a boolean and a byte will "use" the same amount because of byte boundaries... technically speaking a boolean only really needs is a single bit - it's either on(1) or off(0) ... there is no other variance to it. But because of addressing you can't access a single bit in memory... so you have to access the entire byte.... so you end up with 00000000 or 11111111 ... again, this goes back to some of the things that elroy was talking about when we try to coerce a boolean into an integer (or the other way around) ... and it's because of memory addressing issue. So in a practical sense, a boolean should use less memory, but for technical reasons, the realities of it is that a boolean and byte will use the same amount.

    Quote Originally Posted by baka View Post
    also, with byte, we could have "0=undefined,1=false,2=true", to mimic the undefined properties some languages have.
    Are you talking about a byte boolean or a byte byte? I disagree in both cases... a boolean has two values... true or false... it's binary... it shouldn't be allowed to have any other value. It can be nullable, but that's not a value... that's a non-value... an unknown... which is different.

    Quote Originally Posted by baka View Post
    too bad we dont have signed byte, -128 to 127, something i would request in VB7.
    Ick.... I can't even fathom a reason for needing a negative byte...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    test:
    Code:
    Private Type MyBool
    var1(5000) As Boolean
    var2(10000) As Boolean
    End Type
    
    Private Type MyByte
    var1(5000) As Byte
    var2(10000) As Byte
    End Type
    
    Dim MBool As MyBool
    Dim MByte As MyByte
    
    Private Sub Form_Load()
        Dim FileSave%
        
        FileSave = FreeFile
        Open App.Path & "\Bool" For Binary Access Write As #FileSave
            Put #FileSave, , MBool
        Close #FileSave
            
        Open App.Path & "\Byte" For Binary Access Write As #FileSave
            Put #FileSave, , MByte
        Close #FileSave
    End Sub
    Bool is 30 004 bytes
    Byte is 15 002 bytes

    maybe in memory they are the same, but still it counts.

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

    Re: whether boolean has a symbol ?

    Hmmm, I should probably stay out of this memory debate, but I'll weigh in anyway.

    Personally, for me, I think it'd take millions (or maybe even billions) of "flags" (i.e., boolean indicators) before it'd make any significant difference with respect to memory usage. And, if I had some program that needed that, I'd possibly consider using individual bits as Techgnome suggested. I'd possibly write some function that kept a static internal array of bytes, where you could ask it questions like, "is the 126543940th bit on?" A function something like "Function IsBitOn(lngBitNumber) As Boolean". It'd probably be slightly slower than either a Boolean or Byte array, but I doubt it would be much slower, and it'd be far more memory efficient.

    Now, regarding memory boundaries, Techgnome is correct with respect to individual variables. Other than within UDTs and Arrays, as far as I know, variables always start on a four byte boundary in compiled code. However, in p-code, they might start on any two byte boundary. As a quick check of that, we might do the following:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim bb1 As Byte
        Dim bb2 As Byte
    
        MsgBox Hex$(VarPtr(bb1))
        MsgBox Hex$(VarPtr(bb2))
    
    End Sub
    

    You're certainly welcome to try it with other variable combinations.

    Now, if we're talking about arrays or UDTs, things are different. In arrays, the individual elements will always be contiguous, based on the type of variable. UDTs can get complex, but if we're only talking about Bytes, Integers, or Booleans, they are actually also aligned contiguously, Bytes on 1 byte boundaries, Integers and Booleans on 2 byte boundaries.

    Here's some code to test a UDT for Bytes:

    Code:
    
    Option Explicit
    
    Private Type udt
        bb1 As Byte
        bb2 As Byte
    End Type
    
    
    Private Sub Form_Load()
        Dim u As udt
    
        MsgBox Hex$(VarPtr(u.bb1))
        MsgBox Hex$(VarPtr(u.bb2))
    
    End Sub
    

    You're welcome to test it for Booleans and Integers as well.

    So, to answer the question about memory, it depends on how we structure our code.

    -----

    Regarding signed versus unsigned, I truly wish we had a full compliment of both signed and unsigned for all of Byte, Integer, Long, and LongLong. For me, it'd be great if we had the types SByte, UInteger, ULong, and ULongLong. I think the meaning is self-evident. And maybe even UByte (equivalent to Byte), SInteger (equivalent to Integer), and SLong (equivalent to Long). But I suspect that's all wishful thinking.

    Best Regards,
    Elroy
    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.

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

    Re: whether boolean has a symbol ?

    If I were truly worried about memory usage, here's what I'd do. And yeah, I used a Byte array as my storage.

    Code:
    
    Option Explicit
    '
    Dim MyBits() As Byte
    Dim MyBitsDimmed As Boolean
    '
    
    
    Private Sub Form_Load()
    
        Debug.Print IsBitOn(12345)
        IsBitOn(12345) = True
        Debug.Print IsBitOn(12345)
        IsBitOn(12345) = False
        Debug.Print IsBitOn(12345)
    
    End Sub
    
    
    
    Private Property Get IsBitOn(iBit As Long) As Boolean
        ' iBit is zero based, but can't be negative.
        '
        Dim iByte As Long       ' Also zero based.
        Dim bbBit As Byte
        '
        If iBit < 0 Then Error 5
        '
        iByte = iBit \ 8&
        bbBit = iBit Mod 8&
        '
        If Not MyBitsDimmed Then Exit Property          ' Return False.
        If UBound(MyBits) < iByte Then Exit Property    ' Return False.
        '
        IsBitOn = MyBits(iByte) And bbBit
    End Property
    
    Private Property Let IsBitOn(iBit As Long, bValue As Boolean)
        ' iBit is zero based, but can't be negative.
        '
        Dim iByte As Long       ' Also zero based.
        Dim bbBit As Byte
        '
        If iBit < 0 Then Error 5
        '
        If Not bValue Then
            If Not MyBitsDimmed Then Exit Property          ' Nothing to do.
            If UBound(MyBits) < iByte Then Exit Property    ' Nothing to do.
        End If
        '
        ' Make sure we've got the space.
        iByte = iBit \ 8&
        If Not MyBitsDimmed Then
            ReDim MyBits(iByte)
            MyBitsDimmed = True
        Else
            If UBound(MyBits) < iByte Then ReDim Preserve MyBits(iByte)
        End If
        '
        ' Set our bit, True or False.
        bbBit = iBit Mod 8&
        If bValue Then
            MyBits(iByte) = MyBits(iByte) Or bbBit
        Else
            MyBits(iByte) = MyBits(iByte) And Not bbBit
        End If
    End Property
    

    Take Care,
    Elroy
    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.

  26. #26
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    not sure theres a benefit to store each boolean as bit. it will only slow it down and we are talking about very little memory gain.
    but, as my example above, 15k or 30k theres a slice difference.
    i will be using a UDT that contains +50 variables, strings, integer, single, bytes etc.
    theres will be unknown amount of arrays that will use that UDT. it could grow above 1000 that need to be in memory and also be able to load/save.
    sure i will just use around 10 boolean for each UDT, so, say its 1000 items, it will just be 20k of memory if boolean and 10k if byte, its not much.
    but, since byte and boolean are equal in speed and it byte is ½ the size of boolean. byte is better than boolean.

  27. #27
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: whether boolean has a symbol ?

    Quote Originally Posted by Elroy View Post
    I'd possibly write some function that kept a static internal array of bytes, where you could ask it questions like, "is the 126543940th bit on?" A function something like "Function IsBitOn(lngBitNumber) As Boolean". It'd probably be slightly slower than either a Boolean or Byte array, but I doubt it would be much slower, and it'd be far more memory efficient.
    Bit-packing... it's actually a common thing to do ... or used to be back in the day. Most commonly I've seen it used for is combining flag values.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  28. #28
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: whether boolean has a symbol ?

    duplicate
    Last edited by reexre; Oct 19th, 2018 at 07:37 PM.

  29. #29
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: whether boolean has a symbol ?

    Quote Originally Posted by Elroy View Post
    .....

    Just to make sure the code-order wasn't somehow making a difference, I put the Byte test at the bottom, but the results were approximately the same. Therefore, there's certainly no advantage to using Byte variables as Booleans. And, there doesn't seem to be any advantage to trying to match our register sizes either.

    Then, it dawned on me that running as machine code (rather than p-code) might make a difference, so I compiled it. Compiled, here are the results I got (two runs):

    Code:
    Instantiate:  264123.701080343 
       Byte:  0.0437085754820146
    Boolean:  0.0444080176530406 
    Integer:  0.151188194809947 
       Long:  0.149570871435571 
       
    Instantiate:  264127.387196706 
       Byte:  0.0439036753377877
    Boolean:  0.0428782156668603
    Integer:  0.147472180018667 
       Long:  0.148384226427879

    So, the Byte type definitely ran better compiled, and Integer and Long depreciated with respect to Boolean. Therefore, when considering either compiled or p-code, it seems that Boolean is our best choice.

    I compiled with
    Remove Array Bounds
    Remove Integer Overflow
    Remove Floating Point Error
    Allow Unrounded Floating Point Operations
    Remove Safe Pentium(tm)FDIV Checks

    My Results: (32 bit - Windows7 )
    Code:
    Instantiate:  2552,35171440459 
       Byte:  6,15673167681052E-02 
    Boolean:  0,200070566750583 
    Integer:  0,187948178875558 
       Long:  0,226488845830318
    Byte is very much faster ! ( and I'm wrongly used to use "long". I have to change my habit ... )

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

    Re: whether boolean has a symbol ?

    Reexre, you're correct. I did the same on my machine and got the following. I took off the scientific notation, just for ease of comparison:

    Code:
    Instantiate:  344735.926856138 
       Byte:  0.030222249799408 
    Boolean:  0.156855208508205 
    Integer:  0.156332267913967 
       Long:  0.154324380040634 
       
    Instantiate:  344741.889027181 
       Byte:  0.0297044145409018 
    Boolean:  0.149831612361595 
    Integer:  0.149959977192339 
       Long:  0.151075146219227
    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.

  31. #31
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: whether boolean has a symbol ?

    conclusion, byte > boolean.
    faster, less storage and can have more that just true/false.

  32. #32
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: whether boolean has a symbol ?

    The test doesn't show the access speed rather the compiler features. Just swap Byte and Boolean cycles and get:
    Instantiate: 8332,82810399087
    Boolean: 6,91934310088982E-02
    Byte: 0,244156031003513
    Integer: 0,244793053307149
    Long: 0,244284608796079
    The same is applicable for the other types

    ADDED:
    Code:
    Option Explicit
    
    Const cnt As Long = 100000000
    
    Dim cTim    As TimerEx
    Dim dLng    As Double
    Dim dInt    As Double
    Dim dByt    As Double
    Dim dBln    As Double
    
    Private Sub Form_Load()
    
        Set cTim = New TimerEx
        
        Open "timing.txt" For Append As 1
        
        Print #1, "Instantiate: "; cTim.Value   ' Just to get it instantiated.
    
        test_long
        test_bool
        test_byte
        test_int
        
        Print #1, "Integer: "; dInt
        Print #1, "Byte: "; dByt
        Print #1, "Boolean: "; dBln
        Print #1, "Long: "; dLng
        
        Close 1
        
    End Sub
    
    Private Sub test_long()
        Dim l As Long
        Dim dStart As Double
        Dim idx As Long
    
        dStart = cTim.Value
        For idx = 1& To cnt
            l = Not l               ' Flip bits on a Long type.
        Next
        dLng = cTim.Value - dStart
        
    End Sub
    
    Private Sub test_bool()
        Dim b As Boolean
        Dim dStart As Double
        Dim idx As Long
        
        dStart = cTim.Value
        For idx = 1& To cnt
            b = Not b             ' Flip bits on a Byte type.
        Next
        dBln = cTim.Value - dStart
        
    End Sub
    
    Private Sub test_byte()
        Dim bb As Byte
        Dim dStart As Double
        Dim idx As Long
        
        dStart = cTim.Value
        For idx = 1& To cnt
            bb = Not bb               ' Flip bits on a Boolean type.
        Next
        dByt = cTim.Value - dStart
        
    End Sub
    
    Private Sub test_int()
        Dim i As Integer
        Dim dStart As Double
        Dim idx As Long
        
        dStart = cTim.Value
        For idx = 1& To cnt
            i = Not i               ' Flip bits on an Integer type.
        Next
        dInt = cTim.Value - dStart
    
    End Sub
    Instantiate: 9382,40907335988
    Integer: 7,09260534531495E-02
    Byte: 7,06742756410677E-02
    Boolean: 7,58618762993137E-02
    Long: 0,076424098593634

  33. #33
    Fanatic Member
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    678

    Re: whether boolean has a symbol ?

    Quote Originally Posted by The trick View Post
    The test doesn't show the access speed rather the compiler features. Just swap Byte and Boolean cycles and get:
    I had noticed/suspected it.

    Thanks for your test code.
    My results with your code: (Compiled 32bit win 7 - Optimize for speed - pentium pro - Advanced options checked )
    Instantiate: 643,426585384838
    Integer: 3,36052078530429E-02
    Byte: 0,037610823142586
    Boolean: 3,37719438613249E-02
    Long: 3,16118828249046E-02

    Instantiate: 645,047662791833
    Integer: 3,18351194235902E-02
    Byte: 3,08394446821012E-02
    Boolean: 3,14135380946254E-02
    Long: 3,20350445900885E-02

    Instantiate: 723,857386978076
    Integer: 3,19137461194714E-02
    Byte: 3,14526538879818E-02
    Boolean: 3,09117496336739E-02
    Long: 3,24937661672493E-02

    Instantiate: 731,051777062104
    Integer: 3,19105852472603E-02
    Byte: 3,25838510249241E-02
    Boolean: 3,11764726800448E-02
    Long: 3,21883468915303E-02
    So ... not a lot of difference between types. Boolean (Byte) seems the best.

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