Predict The Output III

How many did you get right?

  • Zero

    Votes: 6 37.5%
  • 1

    Votes: 2 12.5%
  • 2

    Votes: 6 37.5%
  • 3

    Votes: 2 12.5%

  • Total voters
    16
Re: Predict The Output II

Hmmm... I was expecting the output to be boolean... that surprised me! Will be looking forward to seeing an explanation of this one!
I was expecting the same. So as you know I am one of the 0's. :biggrin:
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Re: Predict The Output II

...I don't get why MS would program it to behave like that.
Upon further reflection I now get why the NOT operator does what it does. The algorithm makes perfect sense in the context of the integer values of TRUE & FALSE in VBA. Undoubtedly, I'm spoiled by the Boolean worksheet functions which handle the coercion a little more intelligently. That was a good little excercise, Colin. Always good to know where there's a low-flying beam just waitin' ta smack me in the knoggin.
 
Last edited:
Re: Predict The Output II

I like the jokes, Rory. Makes me wonder if you do cryptic crosswords; would suit I think.
 
Colin, when are you providing the analysis - this went so far above my head my neck still hurts from looking at it.
 
Colin, when are you providing the analysis - this went so far above my head my neck still hurts from looking at it.
Should you not want to wait that long -- now that we're onto the second page, here are a couple of hints: think binary & bit math for the AND & OR operators. For the NOT, just type ? true + 0 into the immediate window and then think about what you need to do arithmatically to always jump back and forth between the integer values of TRUE & FALSE.
 
Last edited:
Hi Luke,

Colin, when are you providing the analysis - this went so far above my head my neck still hurts from looking at it.

I'll post the explanation tomorrow evening. I don't want to post it yet in case it spoils the thread for anyone who hasn't seen it.

Glad you're all enjoying it!! :)
 
Missed (Not 2) also...
I expected the answer to be 32,765 :lol:
 
Re: Predict The Output II

I like the jokes, Rory. Makes me wonder if you do cryptic crosswords; would suit I think.

Yep - I like to do the Times on the train home in the evenings.
I still think anyone who likes my jokes should worry though...
 
Re: Predict The Output II

I'm reminded of the (peculiar?) British humour & the clever advertising that I noticed when I travelled on trains in London. I don't think it travels well - across oceans!
 
Time for the explanation! :)


As you may have guessed from hints on earlier posts, this thread is all about bits. But what is a bit? We're all used to the base 10 (decimal) system (digits 0 to 9) and most of us have seen the base 2 (binary) system (digits 0 and 1). A bit is a base 2 digit, either 0 or 1 (off or on).

Sometimes you may have declared a variable in your VBA code as a Byte data type. You can see in the VBA helpfile that a Byte is an unsigned, eight bit number ranging in value from 0 to 255. All of this is easier to understand when we summarise as follows:

Rich (BB code):
Decimal     Binary (Byte)
0           0000 0000       'smallest value
1           0000 0001
2           0000 0010
3           0000 0011
4           0000 0100
...etc...
Each 0 and 1 in the binary column is a bit, and eight bits makes a byte. It's that simple!







Let's revisit the code:
Code:
Sub Predict_The_Output_III()
 
     Debug.Print 2 Or 4
 
     Debug.Print 2 And 4
 
     Debug.Print Not 2
 
End Sub

What data type(s) are these numbers? Are they bytes? We can determine this by using the TypeName() function:
Rich (BB code):
Debug.Print TypeName(2) 'returns Integer
Debug.Print TypeName(4) 'returns Integer
We can also use the TypeName() function to see that the output of each these operations is an integer.


Okay, so we're dealing with integers, not bytes here. You can see in the VBA helpfile that, in VBA, an integer is a signed, sixteen bit number ranging in value from -32,768 to 32,767. The 'sign factor' makes these slightly different to bytes: the first bit (highlighted in red) determines whether the number is negative or positive.
Rich (BB code):
Decimal     Binary (Integer)
-32768      1000 0000 0000 0000     'largest negative value
...etc...
-3          1111 1111 1111 1101
-2          1111 1111 1111 1110
-1          1111 1111 1111 1111
0           0000 0000 0000 0000
1           0000 0000 0000 0001
2           0000 0000 0000 0010
3           0000 0000 0000 0011
4           0000 0000 0000 0100
5           0000 0000 0000 0101
6           0000 0000 0000 0110
...etc...
32767       0111 1111 1111 1111     'largest positive value
I've included a few more numbers there so we can refer to them later on.




So far so good, but what does all of this have to do with OR, AND and NOT? OR, AND and NOT are all bitwise operators: they manipulate bits. We can use truth tables to summarise the results of the operations:

OR has the following truth table:
Rich (BB code):
0 Or 0 = 0
1 Or 0 = 1
0 Or 1 = 1
1 Or 1 = 1
AND has the following truth table
Rich (BB code):
0 And 0 = 0
1 And 0 = 0
0 And 1 = 0
1 And 1 = 1
NOT has the following truth table
Rich (BB code):
0 = Not 1
1 = Not 0
NOT is slightly different to OR and AND because it only operates on a single bit: it is an unary operator.




Now we are armed with this information let's try to predict the outputs of the procedure.

Firstly
Rich (BB code):
Debug.Print 2 Or 4
Let's write the integers 2,4 in binary:
Rich (BB code):
Decimal     Binary
2           0000 0000 0000 0010
4           0000 0000 0000 0100

If we apply our OR truth table to each bit we get the following:
Rich (BB code):
Decimal     Binary
2           0000 0000 0000 0010
4           0000 0000 0000 0100
=
?           0000 0000 0000 0110
If you look back at the integer summary table you can see that ? = 6 which is the output in the immediate window if you run the procedure.



Secondly
Rich (BB code):
Debug.Print 2 And 4
If we apply our AND truth table to each bit we get the following:
Rich (BB code):
Decimal     Binary
2           0000 0000 0000 0010
4           0000 0000 0000 0100
=
?           0000 0000 0000 0000
If you look back at the integer summary table at you can see that the output ? = 0



Lastly
Rich (BB code):
Debug.Print Not 2
If we apply our NOT truth table to each bit we get the following:
Rich (BB code):
Decimal     Binary
2           0000 0000 0000 0010
=
?           1111 1111 1111 1101
If you look back at the integer summary table at you can see that the output ? = -3



Hope that explains the mystery!
 
Last edited:

Forum statistics

Threads
1,222,646
Messages
6,167,311
Members
452,109
Latest member
nathanle89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top