Results 1 to 6 of 6

Thread: [RESOLVED] MsgBox(256 >> 2) In VB6

Threaded View

  1. #6
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: MsgBox(256 >> 2) In VB6

    In base 10, you can do *10, and often school children are taught the trick just add that many zeros. Thus 30*10 is 300, 20*100 is 2000, and so on. These are powers of 10. 10^1 and 10^2 respectively(and 10^3 is 1000).

    Binary(base 2) works the same way with powers of 2. 2*2=4 which in binary is B10 to B100, 2*4(B10*B100) = 8(B1000), 2*8(B10*B1000) = 16(B10000), and so on. The same works with division, only removing digits.

    To rotate, you can mask the bits with AND, and shift them(multiplication or division with powers of 2), then combine them back with OR.

    For example, lets take the number(assuming type byte) 170, in binary that's B10101010. Now lets figure out how to rotate it. To create a bit mask for the AND portion, we'll take our power of two and subtract one. Lets say we want to shift it 2 bits to the right, that's 2^2 or 4(B100), subtract 1, gives us 3(B11), that's the mask we need. So then take (3 And 170), which is 2(B10). Now we need to shift that to align to the left side of a byte. 2^8 is 256, which would shift an entire byte. We need two bits less than that. So: 2^(8-2) is 2^6(which is 64). So then, we get:

    vb Code:
    1. ShiftRight = 4 'should be 1 to 7, ideally
    2. lNumber = 171  'should be a byte 0-255
    3. lNumber = (lNumber \ 2 ^ ShiftRight) Or (((2 ^ ShiftRight - 1) And lNumber) * 2 ^ (8 - ShiftRight))
    4. '         'normal shift            ' +  'create mask         and apply it ' 'shift the masked bits'
    5. Debug.Print lNumber

    Again, this is only designed for Byte. Other types are going to be signed, and you'll have to deal with the sign bit.

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