Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Operations

Status
Not open for further replies.

yumbelie

Programmer
Dec 31, 2002
96
GB
Hi,

Could someone explain how using a binary operation, in this case AND is useful:

If I take the binary for 11 and the binary for 13, and AND the results:

1101 (11)
1011 (13)
-----
1001 (9) - Is this TRUE or FALSE?

What exactly does it acheive? I'm asking because a snippet of code I've been given shows binary operations, basically, a number is taken, if the number is negative it is AND'ed with 128 and converted to ASCII Charater equivilent.

When the character string is to be converted back into a number, the string's ASCII Code equivilent is AND'ed with 128, and this is used to set a variable so that we know to make the number negative. I just don't understand how this is working. As I see it (obviously wrongly), if you AND two numbers, unless they are the identical (in binary) the result should be false?

Any help appericated :)

Thanks
 
Hi,

Logical operators perform a bitwise comparison on numbers. AS you can see from your example the 20 and 23 places are both TRUE -- hence 1001

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
There are two types of numbers you're dealing with: signed (numbers that can be negative) and unsigned (numbers that are only positive). The range in the ASCII chart is 0-255--this is an 8-bit unsigned number. When you use an 8-bit SIGNED number, the range is seemingly cut in half -128 to 127--still 255 number values though. A signed number is stored with the left-most bit indicating whether the number is positive or negative--when dealing with a 8-bit number, this is the 8th bit, which has a decimal value of 128. In other words, by toggling the 8th bit in a 8-bit signed number, you change it's sign. Hopefully the following illustrates this:

unsigned 8-bit numbers
00000001 = 1
10000001 = 129
11111111 = 255

signed 8-bit numbers
00000001 = 1
10000001 = -1
11111111 = -128
 
The point here is that the AND is not performed on the actual values, but rather the individual binary bits that go to make that value...

mmilan
 
Thanks for the responses, but if say I do: 68 And 63 - does this mean it will return True because the bits required to construct 63 are set? For instance


Chr(68 And 63)

?

Thanks.

 
It will do this
[tt]
68 = 1000100
63 = 0111111
-------
68 AND 63 = 0000100
= 4 (decimal)
[/tt]
 
Ah thanks, didn't realise you worked the binary out right to left (etc <-- 8, 4, 2, 1) , not left (1,2,4,8 --> etc) to right.

 
: 68 And 63 - does this mean it will return True because the bits required to construct 63 are set?

If you do anything AND anything, VB will treat any non-zero result as TRUE, if it is inside a bracket.

In some languages, TRUE is 00000001
In some, TRUE is 11111111
This is especially tricky when using (say) VB and C Dlls.
A C DLL may return TRUE as 11111111 which is VB-Speak
for -1
Just something to remember...
 
Jeff,

If you do a LOGICAL operation on numbers (A And B, A Or B), the result is a NUMBER, NOT True or False since a bitwise comparison is performed.

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top