|
-
Aug 30th, 2004, 09:09 AM
#1
First stuff for the new VB6 site
I wrote these articles today at work in my spare time. They'll be one of the most generic content on the site I'm working for, and it'd be nice if you can give comments and such before I put the site up (and I guess it might take time, these are just pure text files). Anyways, here they are as they currently are:
Behind the binary numbers
In my opinion, one of the most important things for a programmer to know are the numbers. If you can't handle math, you can't do very good stuff. Why? Well, you don't know what happens to the numbers, you don't really know the stuff happening "behind the scenes", unless you understand the numbers.
Binary numbers are simple after you get used to them. Binary is also the way computer processes the data, even though this doesn't say all that much to a human. A byte consists of eight bits, bits that are binary. A byte can have a value between 0 - 255. But why? Lets have a few examples.
00000001 - this is 1 in decimals. Quite simple.
00000010 - this is 2 in decimals. How come?
For humans, the smallest bit is in the right. The bits enlarge cumulatively from the smallest to biggest. Thus:
00000100 - 4
00001000 - 8
00010000 - 16
00100000 - 32
01000000 - 64
10000000 - 128
When we add all these values together, we get 255. 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128. But how do we get these add-ups? Well, most people have guessed it already:
00000011 - 3
00000101 - 5
11000000 - 192
Now you should somewhat understand how binary values work. I hope you understand the relation to bytes, integers and longs as well 
Binarywise operators
You might have heard of these operators before: And, Or and Xor. But what these actually do? As you can guess from the header, they have something to do with binary. I try to go as easy as one can go with them.
First of all, lets have two example values:
A = 11111000 - 248
B = 00011111 - 31
Now, lets play with A and B using these operators.
And makes a comparison for active bits (1) and if both bits in the same values are active, they are left active. If they're not active, the resulting value has these bits turned off.
A And B = 00011000 - 24
Or works differently. It check for active bits and makes them active in both values.
A Or B = 11111111 - 255
Then we have Xor. It checks for difference and sets non-matching bits active and turns the others off. Thus:
A Xor B = 11100111 - 231
Then follows a summary:
And:
0 to 0, result is 0
0 to 1, result is 0
1 to 0, result is 0
1 to 1, result is 1
Or:
0 to 0, result is 0
0 to 1, result is 1
1 to 0, result is 1
1 to 1, result is 1
Xor:
0 to 0, result is 0
0 to 1, result is 1
1 to 0, result is 1
1 to 1, result is 0
I hope this is clear enough and the world of binaries opens to you 
Hex and their relation to binary
I write a separate article about hex values, but I want to tell about the relation to binary values in this article. Hex values have 16 different numbers (the everyone's known decimal system has 10 different in comparison). These values are 0 - F (0 - 15). So, what is the relation to binaries? Lets take a look at number 255:
Binary:
11111111
Hex:
FF
Can anyone see a similarity? If not, I'm going with an explanation. The lowest four bits are 1, 2, 4 and 8. When we add these together, we get 15. Someone might have a bell ringing at this point. So, we split both values into two pieces:
Upper bits: 1111 - F
Lower bits: 1111 - F
The relation should be rather clear. Knowing this, it shouldn't be much of a trouble to convert values to hex format. There is only one question one might have: how do we make the upper bits (16, 32, 64 and 128) seem like the lower bits? Luckily, VB has something we can work with: operator \
\ looks how many times a value fits within another. The lowest bit in the upper bits is 16. Thus, if we use \ 16 to the upper bits, we make them the same as the lower bits.
So, to rip the bits:
UpperBits = (ByteValue And &HF0) \ &H10
LowerBits = ByteValue And &HF
Wait wait wait a second! Why And? Well, we wanted to rip the active bytes and that is exactly what And is doing. Also, using hex values is much clearer (binarywisely thinking) than using decimals, because of the clear relation I told about before. Basically we ignore the bits we don't want to use and get the bits we want to.
Bitshifting
One thing missing from Visual Basic are the real bitshifting operators. Bitshifting is a rather simple operation:
A = 01110000
Result = A >> 1
The code above moves bits to the right by one bit. Or, it would if the operator would exist in Visual Basic. The result of the above is:
Result = 10111000
As you can see, the bits moved to right and the active bits appeared from the left. This is how the operator works in C/C++
A = 00001110
Result = A << 1
Result = 00011100
This time active bits didn't appear, though the bits did move to the left as you could guess. Now we have only one question: how to imitate this behavior on Visual Basic?
We're not completely without tools to work with. We have a nice operator: \. This is a very fast to execute in compiled code (and so are And, Or and Xor, if I didn't tell you) so it is almost as good as bitshifting operator. But you must do a bit more work to get the wanted results:
By one bit to the right:
Result = (A \ &H2) Or &H80
Why Or? Well, we need to set the highest bit active. Hex 80 is the same as the highest bit.
By two bits to the right:
Result = (A \ &H4) Or &HC0
&HC0? How come? &H80 + &H40 = &HC0. A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.
Then how about shifting to the left? First of all, we must turn off the bits we don't want to move, to prevent making too big values. Then we simply use * to move the bits.
By one bit to the left:
Result = (A And &H7F) * &H2
By two bits to the left:
Result = (A And &H3F) * &H4
Whenever you need to do bitshifting, I suggest to do it this way instead of making a separate function: calling a function is too slow task to do. Just comment your code when you do this so someone else can understand better what you're doing, too.
Integers and Longs in VB
One of the major problems with Visual Basic is that 16-bit (Integer) and 32-bit (Long) values within VB are always signed. This means, the highest bit is reserved for telling if a value is negative or not. This makes bitshifting a bit harder for these values. Luckily And, Or and Xor can skip this problem. Thus, with integers:
IsHighestBitActive = (Value And &H8000) \ &H8000
And with longs:
IsHighestBitActive = (Value And &H80000000) \ &H80000000
A more cheatty way to check if the highest bit is active:
If Value < 0 Then HighestBitIsActive
Hmm... I guess I've written quite a bit about binary by now. If you have questions to ask or suggestions to give, there is a page where you can find out how to contact me and links to places where you can get more help.
---
End of the first article. I post the other one into separate message.
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
|