Results 1 to 7 of 7

Thread: [RESOLVED] Read / Write Bit Operations

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Resolved [RESOLVED] Read / Write Bit Operations

    Hello,

    I'm not good with Bit/Byte operations & I'm looking for a solution.

    I need to be able to read/modify/save bits from a byte.
    For instance, the read the 2nd bit and change it to either 0 or 1 and then the whole thing back out to as a byte.

    Any ideas?

    Currently, I can already Read the Bits, but haven't figured out how to modify & resave out.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Read / Write Bit Operations

    The 1st bit of a byte is 2^0, 2nd bit is 2^1, 3rd is 2^2, 4th is 2^3, etc
    Another way to look at it is that 1st bit is 1, 2nd bit is 2, 3rd bit is 4, 4th is 8, etc, doubling as you extend.

    So 2nd bit (2^1) of a byte....
    To return it: Value And 2
    To set it: Value Or 2 and to clear it: Value And Not 2
    To toggle it: Value Xor 2

    For the 3rd bit (2^2)...
    To return it: Value And 4
    To set it: Value Or 4 and to clear it: Value And Not 4
    To toggle it: Value Xor 4
    Last edited by LaVolpe; Feb 11th, 2015 at 08:21 PM. Reason: fixed a typo 2^3 vs 2^4
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Read / Write Bit Operations

    I've been testing but something doesn't quite seem right I think because bit 2 is actually pos 6

    So here is the scenario: The byte in question is for instance: 68 | 0, 1, 0, 0, 0, 1, 0, 0,

    So to read it would it be: debug.print 68 And 64 ?
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: Read / Write Bit Operations

    The location of the bits is done from right to left.
    A Long has 32 bits, an Integer has 16 bits and a Byte has 8 bits, the get the same values you have to start at the right.
    Otherwise you would never now which value "0010" would be.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Read / Write Bit Operations

    As mentioned, bits when printed out as 1's,0's are usually read right to left.

    Another thing you may want to do is to create a look up array to help out (until you are comfortable).
    Code:
    Dim potLUT(1 to 32) As Long ' potLUT = power-of-twos lookup table
    
    ...
        potLUT(1) = 1
        For p = 2 to 31
            potLUT(p) = potLUT(p - 1) * 2
        Next
        potLUT(p) = &H80000000
    Now you can use your array, something like: Debug.Print lValue And potLUT(3) to test 3rd bit. Use zero-bound array if more comfortable with that

    Shifting bits is a touch more challenging only because dealing with the high bit in a long/integer may be in play. But that is a slightly different topic than what you are asking here.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Read / Write Bit Operations

    Oh and another thing we didn't touch on. How to test for multiple bits being set... You OR together the bits to test like so:

    Test 1st & 2nd bit (bit 0 & bit 1): If lValue And (1 Or 2) <> 0 Then one or both bits are set.
    If the return value = (1 Or 2) then both bits are set
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Read / Write Bit Operations

    Perfect. Got it working now. Thanks
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



Tags for this Thread

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