Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Retrieving bits in VB 6

  • 12-08-2003 10:34am
    #1
    Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭


    Does anybody know of a way to retrieve bits in VB6 from an integer? Basically I want to retieve the first bit to see if its 1 or 0, retrieve the second bit to see if its 1 or 0, and so on for the full 16.

    Also signed datatypes: both Integer and Long in Vb6. Does anybody know which bit is set to specify the sign (+/-)?


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    This is probably a bit overkill, I'm sure there's better ways of doing it, but in Java...
    // int a is the integer you want to check
    
    boolean [] vals = new boolean[16]
    
    for(int i = 15; i >= 0; i--) {
        if (a >= (2^i)) {
             a -= 2^i;
             vals[i] = true;
         } else
              vals[i] = false;
    }
    

    I'm sure you can read that, but basically, starting at bit n, if the total of the number is >= the value of 2^n, then bit n is 1, so reduce the value of the number by 2^n and continue for all n down to 0. If total of number is less than 2^n, then bit n is 0, so you just record the 0 and continue.

    At the end you should have an array full of true and false values, etc.

    That's the way I'd do it :)


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Its nearly a couple of years since I worked with java last. In Java ^ is an Xor is it not?


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Evil Phil
    Does anybody know of a way to retrieve bits in VB6 from an integer? Basically I want to retieve the first bit to see if its 1 or 0, retrieve the second bit to see if its 1 or 0, and so on for the full 16.

    Also signed datatypes: both Integer and Long in Vb6. Does anybody know which bit is set to specify the sign (+/-)?

    Have a look at this : http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20151207.html

    Should give you most of what you want on retrieving/manipulating bits. There's a method in one of the final posts called "IsBitSet" which should give you what you want I think.

    As for the structure....VB stores the sign in the left-most bit, and stores its numbers with the most significant digit first (i.e. the same way we write numbers)

    jc


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by bonkey
    As for the structure....VB stores the sign in the left-most bit, and stores its numbers with the most significant digit first (i.e. the same way we write numbers)

    Kind of. Generally we would consider that to be the order, i.e. we would generally talk about/write about/draw the bits of a long as being written that way if so that the number would be:

    11111111111110010110111000000101

    The first bit in the list (normally labelled as bit 31) is the sign bit.

    However the order in memory is a different matter. Breaking the above into octets we have:

    11111111|11111001|01101110|00000101

    This is the correct way to talk about the mathematics of what is going on, but isn't what's happening in the memory.

    If you have to worry about memory representations (rare in VB, but not unheard of) then it isn't meaningful to talk about bit orders (you can't address them) so we might as well use hex for more readability:

    FF|F9|6E|05.

    The actual order in memory will be little-endian:

    05|6E|F9|FF.

    This becomes important if you are using long values in conjuction with CopyMemory, MoveMemory or kludgy struct-based conversions.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    If (a And (2 ^ i)) then
        Debug.Print 1
    Else
        Debug.Print 0
    End If
    

    Theres sample code equivilant to what I ended up doing. Here a is the integer containing the bits and i is the bit being checked for.

    What I'm doing is storing the values of a number of checkboxes in a single integer and then retrieving them. I could go into detail but not unless you really, really want me too.

    But what I'm actually doing is developing an algorithm that has serveral uses.


  • Advertisement
  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    The problem with that pseudo code is that there is no concept of an unsigned type in VB (except for bytes, for which there is no concept of a signed equivalent).

    The following overcomes this for Longs
    Public Function BitTestLong(a As Long, i As Long) As Boolean
        Const HighestBit As Long = 31
        Select Case i
            Case Is < 0, Is > HighestBit
                Err.Raise 5
            Case HighestBit
                BitTestLong = a < 0
            Case Else
                BitTestLong = a And (2 ^ i)
        End Select
    End Function
    
    For Integers you would change the type of a to Integer, i can be left long (since it's the most efficient type), you would also change HighestBit to have a value of 15.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    It might be faster to have a lookup array of the powers of 2 (0, 1, 2, ..., &H40000000, &H80000000) and use that.


Advertisement