Page 1 of 2

The & operator

Posted: Sat Feb 06, 2010 3:27 pm
by SFSxOI
I have a couple of basic questions. I know the answers within the limits of my own understanding but I can't explain my answers suitably for a specific purpose as my own foundation and training is really not one of a programmer. I need to be able to offer an "expert" type of opinion on these specific questions. Maybe seems like a couple of simple questions but important in this case none the less. The PureBasic help provides part of the answer for question 2 believe it or not, but its not explainatory enough for my purpose.

The questons are:

1. Is there a limit for the '&' operator?
2. How is the result stored?

First question:

If there is this:

Code: Select all

Debug 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1
obviously the above result is going to be 1 (#True), but is there a limit on the number of "&'s" that can be performed in a single instance like the above?

Second question:

The help says this for the '&' operator:

"a.w = %1000 & %0101 ; Result will be 0
b.w = %1100 & %1010 ; Result will be %1000
bits = a & b ; AND each bit of a and b and use result in equals operator
a & b ; AND each bit of a and b and store result directly in variable "a"

How is the result stored in variable "a" ?

Thank You

Re: The & operator

Posted: Sat Feb 06, 2010 3:54 pm
by Kaeru Gaman
SFSxOI wrote:a & b ; AND each bit of a and b and store result directly in variable "a"

How is the result stored in variable "a" ?
same as with
a + 1
or
a * 4

Re: The & operator

Posted: Sat Feb 06, 2010 4:40 pm
by SFSxOI
Thanks for the reply Kaeru, but not exactly the answer I was looking for. I realize now that my question wasn't explainatory enough though.

I need the "mechanics" involved of how this happens so that I can explain it to a group of laymen who have no understanding at all. I can explain that: 1 + 1 = 2 because a layman can see that eaisly because one thing is equal to another, but just to say that "a & b ; AND each bit of a and b and store result directly in variable "a" " doesn't explain it because to a layman the "a =" magically appears for some reason and all of a sudden we are saying that for some reason 'a = a & b' when there was no 'a =' to begin with. To a layman this says that 'a = b' when thats not true at all because 'a & b' does not mean that 'a = b'.

I guess I need some sort of "a = b" type of formula or way to demonstrate in writing or graphic verbal explaination how in the statement 'a & b ; AND each bit of a and b and store result directly in variable "a" ' - how the result is stored "directly in variable "a" '

Re: The & operator

Posted: Sat Feb 06, 2010 4:49 pm
by Demivec
SFSxOI wrote:Thanks for the reply Kaeru, but not exactly the answer I was looking for. I realize now that my question wasn't explainatory enough though.

I need the "mechanics" involved of how this happens so that I can explain it to a group of laymen who have no understanding at all. I can explain that: 1 + 1 = 2 because a layman can see that eaisly because one thing is equal to another, but just to say that "a & b ; AND each bit of a and b and store result directly in variable "a" " doesn't explain it because to a layman the "a =" magically appears for some reason and all of a sudden we are saying that for some reason 'a = a & b' when there was no 'a =' to begin with. To a layman this says that 'a = b' when thats not true at all because 'a & b' does not mean that 'a = b'.

I guess I need some sort of "a = b" type of formula or way to demonstrate in writing or graphic verbal explaination how in the statement 'a & b ; AND each bit of a and b and store result directly in variable "a" ' - how the result is stored "directly in variable "a" '
The typical way is that "a & b", by itself is the equivalent of a "Let a = a & b" even though that syntax doesn't exist in PureBasic. It isn't any harder to understand that than to say "a = a & b", which looks like an equation but isn't because the "=" represents an assignment. In other words, it is described as a shortcut that does the same thing as the other way.


How old are the laymen? :wink:


You can illustrate the functioning of the operator with the usual:

Code: Select all

  %1100
& %1010
-------
  %1000

using truth table:

a     b    result
--   --   ------
 1    1      1
 1    0      0
 0    1      0
 0    0      0

Re: The & operator

Posted: Sat Feb 06, 2010 5:47 pm
by Kaeru Gaman
yes, the mechanics is the same as with
a = a + 1
a = b * 4 + c
or whatever.

in PureBasic you can leave the "a =" in the beginning, with any arithmetical operation.
if someone does not understand it, just let him write "a =" in front of it, doesn't matter.

Re: The & operator

Posted: Sat Feb 06, 2010 6:20 pm
by SFSxOI
Thanks guys;

Allow me to explain whats going on some, I wasn't going to do it because the District Attorney told me not to but i've explained it to him that I could not answer the question properly so he says I can seek a proper answer now.

There is this court case i'm involved in as a result of the work I do for a company I work for. It involves a man who hacked into a company network, he is (was) an employee of this company The results of his hacking resulted in the theft of over $100,000.00. The company I work for was contracted by a law enforcement agency to show that he did this. The defense (his lawyer) claimed that the guy could not have done it because of the security software in place, and the company that makes the security software says that it would have not been possible because their software would have stopped, or alerted someone to, the attempt. Our own programmers looked at various methods and could not come up with anyway around the security software company claim, as a result the case would have been lost and the guy would have won the case and not been prosecuted for his crime. However, i got this bright idea that it was possible and I used COMate to show that it was possible through use of WMI ( see > http://www.purebasic.fr/english/viewtop ... &start=150 > page 11 of that thread > posts 1 - 4 on page 11).

Since I was the one to be able to show it was possible I had to testify in court and give "expert opinion". Everything was going fine until this happened:

(the below picks up later in my questioning)

Defense: "This is the method you discovered and claim my client used, correct?"
Me: "yes sir"
Defense: "In this method did you employ any certain skills or other methods to arrive at your conclusions?
me: "yes sir"
Defense: "What were these skills or methods you employed?
me: "I used a programming language called 'PureBasic' and a publically available subset usage of that language called 'COMate'.
Defense: "This PureBasic and COMate is not a mainstream programming language is it?
me: "It is within the community of usage."
Defense: "I see, well, I mean, its not like other programming languages used for most programming tasks which are used to produce products normally sold to consumers?
me: "no sir, but it is used to produce products that are sold to consumers, maybe not the majority of mainstream consumers but there are products sold to consumers which are produced with PureBasic."
Defense: "Is this your method?" (at this point he puts the code I wrote using COMate on a screen)
me: "yes sir"
Defense: "can you explain this part please, specifically the '&' part?" (the defense has programmers available to them as consultants)
(at this part he points to a section in the code where I had used this "If PartA & PartB & PartC = #True")
me: "the parts, PartA and PartB and PartC, are a Bitwise AND"
Defense: "What does that mean, a Bitwise AND ?"
me: "it means the parts are compared bitwise."
Defense: "What is a bit?"
me: "In this case a bit is either valued at 1 or 0, either True or False, 1 being True and 0 being False"
Defense: "What is PartA, PartB, and PartC? Do they have values?"
me: "yes they do."
Defense: "What are the values?"
me: "it depends if a part is true or false. If its true then the value will be 1, if the part is false then then the value will be 0. If all the parts are True then the result is True, or 1, if any of the parts are False then the result is False, or 0."
Defense: "Like 1 +1 = 2 ? Or 1 + 1 + 1 = 3?"
me: "no, not exactly. Its a comparison test to determine a result."
Defense: "So it is like 1 + 1 = 2? I'm confused here."
me: <silent> then i say > "Is that a question? I'm sorry, I don't understand the question."
Defense: "is it possible that a comparison could fail?
me: "I guess its possible, but this is not asking for a failure of any certain thing, its just a comparison of things and the result is going to be what it is."
Defense: "i see, and where is this result given at, show us please if you would in your code?"
me: "its not given anywhere, its just a test for comparing one thing against another. The result of that comparison will determine what happens next. So the result is shown in what happens next after the comparison."
Defense: "But isn't this very obscure?"
me: "I'm sorry, I don't understand the question."
Defense: "Can you explain to the jury just how your code, this part, determines a result?
(at this point I look at the DA and he is giving me that "don't say anything" look)
Defense: "so 1 + 1 does not equal 2? is this what your saying?"
me: "no"
Defense: "When I went to school 1 +1 was equal to 2, but your saying now that it doesn't?" (he is looking at and talking to the jury at this point - you can see the confusion in their eyes)

At this point the DA objects, he says that i'm not a programmer. The defense counters by saying "He was qualified as an expert. His testimony directly impacts the outcome of this case." The DA says "But he wasn't qualified expert as a programmer your honor." The defense counters "he was qualified as a forensic expert in this matter your honor, his methods and conclusions were derived as a result of his forensic work on this case. We contend all methods of collecting evidence or demostrating culpability and rendering conclusions are subject to complete explaination."

The judge ponders this for a second and says "He is qualified as a forensic expert and not as a programmer. However, he also developed a method and used that method to arrive at a conclusion which is within the rhelm of the forensic examination he conducted and as such he should be able to explain his methods completly to support his conclusions just as any expert would need to do."

At this point the DA asks for a recess, and the case continues next week. So you see, I need to be able to explain this in such a manner as to dispell the confusion created by the defense. So thats why i'm asking the questions.

(Strangely, there were no questions about COmate its self at least in an explainatory sense so i'm thankful for that or otherwise i'd have to convice srod to pay us a visit. :) )

So, do you see why I need to be able to explain the '&' part? Since our own programmers could explain what this would be in a mainstream programming language like C++ or something but not how PureBasic determined this they were of little use, and because I have to be able to validate the Purebasic use of this method, I needed to ask the question. The WMI part explained and not confusing at all, its just this one '&' part.

Re: The & operator

Posted: Sat Feb 06, 2010 7:08 pm
by Trond
It's a bit hard when you don't explain what it's for. Isn't it possible to rewrite the code so that it's easyer to understand?

In my opinion, comparing the result of bitwise and with #True is just wrong, as it makes no sense. #True is supposed to be boolean, not bitwise.

You seem to be using boolean values, though:
me: "In this case a bit is either valued at 1 or 0, either True or False, 1 being True and 0 being False"
In that case you should REALLY be using the boolean And, which is very simple to explain. If you use = 1 you make it a no-brainer.

Code: Select all

If (PartA = 1) And (PartB = 1) And (PartC = 1)
  ; Section 1:
EndIf
"If PartA, PartB and PartC are all equal to 1, then the code in section 1, which goes down to the EndIf, is executed. If one or more of PartA, PartB and PartC are not 1 (ie. they are 0) then section 1 is skipped."

If you are not allowed to change your existing evidence, you can present the above code and say that it is functionally equivalent to the other code.
Since our own programmers could explain what this would be in a mainstream programming language like C++ or something
But the & means that same in PB as in C++, where is the problem?

Re: The & operator

Posted: Sat Feb 06, 2010 7:39 pm
by IdeasVacuum
The only way to explain the code in this situation is with carefully drawn step-by-step graphics.

You are showing how something works, kindergarten teacher style (but not condescendingly), no room for confusion or ambiguity.

An "easy-peasy" explanation of logic. Show a very simple example of an arithmetic sum (the one one used by the Defense, 1 + 1 = 2) and below it the graphical explanation of the & logic - importantly, explain what happens next, depending on the logic result. Best to use really simple common-world examples (two or three) that non-programmers will understand. You know, "If the traffic light is green & nobody is in the way = the truck can be driven forwards". "If the kids have behaved well & they have done their chores & they have finished their homework = they can go to the cinema"

It is also worth mentioning that all programming languages have logic methods. It does not matter that you chose to use PB and ComMate, that is simply your preference. You drive a Ford, your neighbor drives a Toyota, they both do the same job of getting people from A to B.

Re: The & operator

Posted: Sat Feb 06, 2010 8:03 pm
by Demivec
You can say that PartA, PartB, and PartC are given a numeric value (I presume only 1 or 0 is possible).

The representation of these values in binary are then compared to each other with the "&" operator. Each digit of the binary representation has a place value just like decimal numbers do and can contain either a one or a zero. Each digit of a binary number is referred to as a bit and the place values start at 1 and double as you move to the left. A bit can be thought of like a light switch that only has two positions, either on or off ; either one or zero.

When the binary numbers are compared with the "&" operator, each bit is compared. If the corresponding bits of each binary number are both one then the result will also contain a 1 for that place value; otherwise it will receive a zero for that bit. Using the analogy of a light-switch where a value of one means the light-switch is on and zero means it is off; each number has several light switches. When the numbers are compared if they each have the corresponding bits "on" the result will also have it's bit "on".

The comparison is made between all three values PartA, PartB, and PartC. If the comparison shows that each of the parts have the rightmost bit "on" or equal to one (represented by the value #True) then the statements in the If/EndIf block will be executed.

Re: The & operator

Posted: Sat Feb 06, 2010 8:33 pm
by skywalk
Very interesting discussion...

The defense attorney is not a programmer.
The defense attorney's questions on programming internals are designed to confuse the jury and trip you up. Since he is not a programmer, seemingly stupid questions can be asked.
You must quickly and confidently respond as such.
Your answers should always be very very short.
Never look to your attorney under cross examination.
Only look to the judge, again with confidence.
"Your honor, the attorney's question is difficult to comprehend."
"Your honor, I do not want to appear as to belittle the intelligence of the defense attorney."
Your answers will be compared and analyzed by the defense's own programmers.

Purebasic is a tool.
A hammer is a tool, regardless of the manufacturer or construction.
However, the quality and effectiveness of any tool is dependent on its application and who is wielding it.
Purebasic is available for purchase by anyone, and even has a demo program for free.
Explain that with some extra work, you could have accomplished the same feat, with another set of tools. That was simply not your goal initially.

Point of note....
It is rare that engineers are on juries, so your audience is not generally knowledgeable on minute details of math and science and if so are usually weeded out of the pool by the attorneys prior to deliberation. The reason is they tend to form early opinions of the trial data as it is being presented. Much harder to control their final decisions - Yay or Nay.

So, it sounds like he is trying to either prove you provided a highly unique solution which could rarely be duplicated. Or he is trying to prove you don't understand your solution and stumbled upon it given you knew the desired outcome.

Lots of fun,
Good Luck

Re: The & operator

Posted: Sat Feb 06, 2010 9:38 pm
by SFSxOI
Trond wrote:It's a bit hard when you don't explain what it's for. Isn't it possible to rewrite the code so that it's easyer to understand?

In my opinion, comparing the result of bitwise and with #True is just wrong, as it makes no sense. #True is supposed to be boolean, not bitwise.

You seem to be using boolean values, though:
me: "In this case a bit is either valued at 1 or 0, either True or False, 1 being True and 0 being False"
In that case you should REALLY be using the boolean And, which is very simple to explain. If you use = 1 you make it a no-brainer.

Code: Select all

If (PartA = 1) And (PartB = 1) And (PartC = 1)
  ; Section 1:
EndIf
"If PartA, PartB and PartC are all equal to 1, then the code in section 1, which goes down to the EndIf, is executed. If one or more of PartA, PartB and PartC are not 1 (ie. they are 0) then section 1 is skipped."

If you are not allowed to change your existing evidence, you can present the above code and say that it is functionally equivalent to the other code.
Since our own programmers could explain what this would be in a mainstream programming language like C++ or something
But the & means that same in PB as in C++, where is the problem?
No, I can't change it now because its part of the record of evidence. Yes, I understand what your saying about the 'AND' instead of the '&', but I wrote the method code to as closely as possible match what i thought the guy used, and fragments of his code recovered from his computer used the '&', in fact that one section was exactly from one of his code fragments, I could not recover the whole code from his computer but was able to recover enough code fragments to give me an idea as to what he was doing when i could piece them together. The code fragments alone were not enough to convict, but when they are put together with my code they give a pretty good idea as to what happened. Basically it was a reconstruction of the crime and the methods used to do it.

Its not that & means the same in other languages, its the fact that PureBasic is not one of those other languages and someone may go to prison as a result of what they did. Its a little more serious then just saying "But the & means that same in PB as in C++, where is the problem?" and assuming that everyone is in agreement with you. It may mean the same, but is it enacted and arrived at the same or by the same common basis, and if its not then its suspect, and then you have to explain it in such a way to a jury of 12 people with no understanding that thinks 1 + 1 = 2 all the time which is not the same as 'If 1 AND 1' or 'if 1 & 1'. In a case like this, common knowledge is not applicapable, and although it may be common knowledge to you and I and the rest of the PB community, in a case like this things have to remain exactly in context and that means explaining how it works with PureBasic and nothing else even if it is the same for other languages. I don't blame the defense for doing this, if i were the guy and could go to prison and get a hefty fine i'd want my attorney trying to discredit the prosecution case too anyway the attorney could and if that means having the prosecution experts explain every little thing and justify it then so be it.

Re: The & operator

Posted: Sat Feb 06, 2010 9:40 pm
by SFSxOI
Demivec wrote:You can say that PartA, PartB, and PartC are given a numeric value (I presume only 1 or 0 is possible).

The representation of these values in binary are then compared to each other with the "&" operator. Each digit of the binary representation has a place value just like decimal numbers do and can contain either a one or a zero. Each digit of a binary number is referred to as a bit and the place values start at 1 and double as you move to the left. A bit can be thought of like a light switch that only has two positions, either on or off ; either one or zero.

When the binary numbers are compared with the "&" operator, each bit is compared. If the corresponding bits of each binary number are both one then the result will also contain a 1 for that place value; otherwise it will receive a zero for that bit. Using the analogy of a light-switch where a value of one means the light-switch is on and zero means it is off; each number has several light switches. When the numbers are compared if they each have the corresponding bits "on" the result will also have it's bit "on".

The comparison is made between all three values PartA, PartB, and PartC. If the comparison shows that each of the parts have the rightmost bit "on" or equal to one (represented by the value #True) then the statements in the If/EndIf block will be executed.

BINGO! Thats it. Thats exactly what I want to say. Thank You :)

Re: The & operator

Posted: Sun Feb 07, 2010 1:07 am
by IdeasVacuum
In my view, it's not quite a "bingo" because the jury will not understand such a wordy description. However, if you actually draw some light switches to demonstrate, that will work -a picture paints a thousand words.

Re: The & operator

Posted: Sun Feb 07, 2010 5:31 am
by Rescator
You might also want to look at this:
http://en.wikipedia.org/wiki/Bitwise_operation

Have the Lawyer look over this wording with you as it may need to be simplified slightly for the court.

You may also say that things like "and &" etc. are basic computer operations that every computer in the world performs billions of times throughout a year, and is as common as using "the it she he" in conversation.

Let's hope you wont have to talk too much about variables and pointers though, if they try to trip you up on that then just state that you are unfortunately not a programming teacher and the court would need a crash course in elementary computer programming. Just state that stuff like that is done all day long by the software in the cellphones of those attending the court proceedings right now.

Or that the court (whats the transcriber/logger called?) actually causes & to be used every time she/he types a letter as pressing a key shorts one or more wires and & is used to determine if say wire 1 and 2 are shorted and if they are then that would mean the key for letter A was pressed, and so on.

And as to that stupid 1+1=2 statement just say that 1+1=2 is always true and not relevant in this case at all and that the Defense's confusion and confusing question confused you. If the defense where to ask the correct question it would receive a correct answer. :)

Asking a non technical question and expecting a technical answer is not possible, likewise asking a technical question and expecting a non-technical answer is also not possible. (it's called technical and expert and so on for a reason)
It's almost like asking a lawyer to explain legal terms so that a layman can understand them. (the court should understand that analogy I hope?)

Also don't forget to clarify that just because you did not use a mainstream programming language does not mean the methods are different, just like the English and the Americans use the words Lift and Elevator, different naming same meaning.
And that there are hundreds of free and commercial programming languages out there and they all have the potential to create the exact same software but by varying methods to achieve the same goals.

It's like gravity. Newton's apple fell, and it may have fallen due to wind, age, tremors, or something could have struck it, but regardless of the method, it still fell.

Maybe you could draw a small chart as someone said?
"If statement A is true and statement B is true and statement C is true then perform these actions." That's as basic as one can explain "If (something) & (something) & (something)" to a layman. (even though it's an expression, the term statement should easily be understood by the court)

The only thing you need to do is make the DA understand what you mean, if he does then he can tell you exactly how to explain that to the court without confusion. (let's hope the DA is clever :)

I'm surprised how patient you are, I'd probably have banged my head against the desk and shouted "it's a frigging & operator, sheesh! *bang*" ;)


It may also be worth mentioning that computer programming tools are becoming advanced enough and computer programing so complex that even today there are programmers out there able to create commercial software without actually understanding how it truly functions. It's like learning to speak without understanding what a verb is. Or learning your ABC but no idea what a consonant is. So explaining what and how binary bits and comparisons work is like asking someone to explain how they are able to hear sounds. Or ask how a speaker works. Or how a mobile phone works.

One thing for sure, that Defense lawyer, I'd have fired him a long time ago if I was the Defendant, seems to have no clue on computing at all other than how to use Word. *laughs*

Oh and: "the company that makes the security software says that it would have not been possible because their software would have stopped, or alerted someone to, the attempt"
Tell the court that just because something is not possible does not mean it is impossible, just that it's not been possible "yet". (ref. Microsoft recently/about to patch a 17 year old security hole? *laughs*)
http://www.computerworld.com/s/article/ ... indows_bug

Re: The & operator

Posted: Sun Feb 07, 2010 7:20 am
by idle
There are only 10 kinds of people in the world those that understand binary and those that don't!