Newbie Questions

B

Bari

I'm currently working my way through "MS VB.NET Step by Step", by Michael
Halvorson, and I have a couple of questions.

1) In the book, they're explaining the following line of code, used in an
encryption algorythm:

msgbox(Asc("A") XOR 50) would display 115, and
msgbox(115 XOR 50) would display 65 (the asc value of the letter A)

The Studio's help description for the XOR operator used in this fashion
(bitwise exclusion on two numeric expressions) follows:
This example uses the Xor operator to perform logical exclusion of the
individual bits of two numeric expressions. The bit in the result pattern is
set if only one of the corresponding bits in the operands are set.

Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Integer
myCheck = (A Xor B) ' Returns 2.
myCheck = (A Xor C) ' Returns 12.
myCheck = (B Xor C) ' Returns 14.
Unfortunately, that's the only explanation given. Do you know exactly what
the XOR operator is doing, there? In the first example, above, it looks
like the result is A-B, and the 3rd example looks like B+C, but I find no
relation in the 2nd example. Obviously, my ideas of the 1st & 3rd are
incorrect. Do you know what kind of math the XOR is actually doing?

2) Also, is there a quick & dirty way to test results of stuff like that?
In command/immediate mode, why does:

? chr("A") and msgbox(chr("A")) get the following response:
The expression cannot be evaluated while in design mode.

-and-

messagebox.show(chr("A")) gets:
Command "messagebox.show(chr("A"))" is not valid

How would I test stuff like that, without actually writing the lines into an
application or form, first & then going to immediate mode, or can I?

I appreciate any help you can offer. Thanks in advance :)

Bari
 
D

Dominique Vandensteen

xor is exculsive or
bitwire operation
0 xor 0 = 0
1 xor 0 = 1
0 xor 1 = 1
1 xor 1 = 0

so
a=10 --> 1010
b=8 --> 1000
c=6 --> 0110

a xor b --> 1010 xor 1000 --> 0010 --> 2
a xor c --> 1010 xor 0110 --> 1100 --> 12
b xor c --> 1000 xor 0110 --> 1110 --> 14


dominique
 
H

Herfried K. Wagner [MVP]

* "Bari said:
I'm currently working my way through "MS VB.NET Step by Step", by Michael
Halvorson, and I have a couple of questions.

1) In the book, they're explaining the following line of code, used in an
encryption algorythm:

msgbox(Asc("A") XOR 50) would display 115, and
msgbox(115 XOR 50) would display 65 (the asc value of the letter A)

The Studio's help description for the XOR operator used in this fashion
(bitwise exclusion on two numeric expressions) follows:
This example uses the Xor operator to perform logical exclusion of the
individual bits of two numeric expressions. The bit in the result pattern is
set if only one of the corresponding bits in the operands are set.

Basic information on (logical) XOR:

<http://en.wikipedia.org/wiki/Exclusive_disjunction>

'XOr' = Exclusive Or, for example:

0101010101111
1110101001010
------------- (XOR)
1011111100101

1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0

When using 'Xor' with integers, notice that all(!) bits will be 'XOr'ed.
 
B

Bari

Thanks to all of you,
I understand the XOR function, now. Do any of you have an answer to the
second question, regarding the immediate/command mode, and testing simple
statements, that are not part of an actual program? Thank you.

Bari
 
O

Ot

You are on the right track. You run a simple test program -- perhaps a
form with one pushbutton.

Then put the MsgBox in the handler for the click event.

Here's one I did to test some code:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim instring As String = "00A1B2C3D4FF"

Dim B(instring.Length / 2) As Byte

For i As Integer = 0 To (instring.Length / 2) - 1

B(i) = System.Convert.ToByte(instring.Substring(i * 2, 2), 16)

Next

MsgBox(Hex(B(0)) & Hex(B(1)) & Hex(B(2)) & Hex(B(3)) & Hex(B(4)) &
Hex(B(5)))

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top