# Masking, the Ins and outs

Status
Not open for further replies.

#### otm

##### Member
Hello Everyone, Not been on here for a while, sorry!

I have an A level electronics exam in a couple of weeks, and one of the papers is on control systems.. one of the things that keeps coming up is masking. However, the teacher that teaches that side of the course is really, really, really bad (it's not just me.. as a class we saw the head of department to express our concern before christmas. and her lower 6th classes are the same). We haven't covered masking, and she doesn't know what to do..

thats why I am coming here!

as part of the course we learn Qbasic, and that what the question relate to..

here is a link to an example paper (with mark scheme):

Question Paper

Mark Scheme

if you look to page 19 you will see what I'm talking about.

what is the AND function, OR function (and any other that I might not realise!)

All I know is that the AND function and's them together and you can use it to seperate bits out, but thats all i know, and that is not a suitable answer.

Thank You,

Owen.

PS. can we have no comments about how hard/easy people think the paper is, it's not hepful this close to the exams......

#### UTMonkey

##### New Member
I am not sure what kind of language the assessors are looking for in your responses but for starters AND and OR are OPERATORS.

Looking at 7. e (ii).
X% = X% AND 8

The above does 2 things, first take the contents of X% and performs a logical AND with a literal number (in this case 8) and store it in variable X% (thereby overwriting the original value of X%).

if X% for example held the number 255 (which in binary is 11111111) and 8 is 00001000, the AND operation would produce a positive 1 if both bits compared had a 1.

11111111 = 255
00001000 = 8
00001000 = 8 is what would be held in X% after the above operation.

The AND operator is ideal if you have a variable but you are only interested in a particular part of it.

For example, take the binary value 11110101 (245 in binary, 0xF5 in hex), lets say you are only interested in the first 4 bits (or nibble) of that value (0101) masking allows you to clear the bits your don't want and leaves the remaining bits in tact (or gives that impression).

So, you need to construct a mask, for this example you would need 00001111, take a look what happens, below.
11110101 = 245
00000101 = The result!

Hope this makes sense.

Mark

Last edited:

#### otm

##### Member
Hello,

Thank you for your response i understand what you've written, it makes sense to me now,

so if after that you had a piece of code saying if X% = 8 then..... you could use it to find the sate of bit4... correct?

what i don't get is the answer they were looking for:

Masks the least significant three bits and
to avoid errors from undefined bits for the two marks

Thank You,

Owen.

#### UTMonkey

##### New Member
Close...

To say "if X%=8" would indeed check bit 4 but what about "if X%=10"?

00001000 = 8
00001010 = 10

Both numbers have bit 4 set but if the number in question was 10 it would evaluate as false! And that is why masking is so useful.

Take a look at this:-

"if (X% AND 8)=8"

Now that is more like it. What you are saying here is take X%, perform the AND operation with the number 8.

Look at the result based on examples of 8 and 10 below.

00001000 = 8
00001000 = AND 8
00001000 = 8 = Result!

00001010 = 10
00001000 = AND 8
00001000 = 8 = Result!

That is an example of using masking to check for certain bit values.

Mark

#### otm

##### Member
hello,

in the mark scheme it says:

Masks the least significant three bits and
to avoid errors from undefined bits for the two marks

so the least significant bits at the 3 binary digits:
------|||
------vvv
00001000

and going on from what you say
to avoid errors from undefined bits for the two marks:
this means it ignores the values of bits 1 2 and 3, but I agree with what you say, if either bit 1 2 or 3 are high, then the whole eoc: subroutine in the program is wrong.

do you know much about OR?

Thank You very much,

Owen.

Last edited:

#### AtomSoft

##### Well-Known Member
Dude its Logic lol

Logic gate - Wikipedia, the free encyclopedia

OR basically is a Test of ZERO. If you OR 2 items it will always be a 1 unless both are 0

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

Lets say you want to make a few bits HIGH you would:
Code:
A = 1100 0000 OR'ed with B (below)
B = 0001 0101
-------------------
1101 0101 (outcome)

Last edited:

#### otm

##### Member
I know its logic, and I know how an OR gate work, but what I do know is that it does funny things when using it in coding. Looking in the mark scheme the answer for

What effect does the statement PCOUT% = PCOUT% OR 64 have on the
value of PCOUT%?

is

Sets bit D6 to logic 1 without affecting the other bit values

which I don't understand...

and the next few parts are confusing...

(ii) What is the function of the statement Y% = Y%/16?

(iii) What effect does the statement PCOUT% = PCOUT% AND 191 have on the
value of PCOUT%?

(ii) It moves the bits in Y% to the right, changes bits on D4 to D7
to D0 to D3
(iii) It sets D6 to logic 0

I just dont get it!!! Part 3 I think I am understanding but if that came up in my exam, I'm not sure I could answer it.....

Cheers,

Owen.

#### Pommie

##### Well-Known Member
Because we are working in binary, dividing by two moves all the bits right by one place.

234 = 11101010
234/2 = 117 = 01110101

Dividing by 16 is the same as dividing by 2 four times and so it shifts the number right 4 binary places. This is the same as dividing by 10 in base 10. 2345/10 = 234. Note we drop the bits that fall of the end.

ANDing with 191 is easier to see if we convert 191 to binary,

191 = 10111111

All bits set except bit 6 and so the effect is to reset bit 6 of any number it is ANDed with.

Mike.

#### otm

##### Member
Pommie,

Thank You for your great reply.. I can see how that works!!!

Heres what I understand of the Divide process:
11101010 <--original
01110101 <--divide by 2
00111010 <--divide by 4
00011101 <--divide by 8
00001110 <--divide by 16
^to^
D4 to D7 are now 0 and D0 to D3 are changed

Thank You Pommie and UTmonkey...

Owen.

Last edited:
Status
Not open for further replies. 