Share
Embed
Transcript
24 slides
-
Bit Masking ▪ Bit masking is the ability to modify
individual bits in a word in a variety of ways, we can modify them as follows: – Switch a bit ON – Toggle a bit – Switch a bit OFF -
Bit Masking – Switching a bit on ▪ Thinking of
how to turn a singe bit on we could do a simple OR operation: ▪ Regardless of the value of x when we or it with a 1 the result will always be a 1. x or 1 = 1 -
Bit Masking – Switching a bit on ▪ Let’s say
for instance we have the following binary number and we want to switch the 0 on as indicated: 10110011 -
Bit Masking – Switching a bit on ▪ We now
introduce a new number underneath: 10110011 00000001 ▪ Then we bit shift it left until it lines up with the one we want to switch on. -
Bit Masking – Switching a bit on ▪ We then
do an OR operation with these two numbers: 10110011 00000100 OR -
Bit Masking – Switching a bit on ▪ We then
do an OR operation with these two numbers, the resulting number looks similar but we have switched the individual bit ON. 10110011 00000100 OR 10110111 -
Bit Masking – Toggle a bit on/off ▪ To toggle
a bit on or off we need to XOR our number with a 1, look at the truth table for XOR: A B Result 0 0 0 0 1 1 1 0 1 1 1 0 -
Bit Masking – Toggle a bit on/off ▪ To toggle
a bit on or off we need to XOR our number with a 1, look at the truth table for XOR: A B Result 0 0 0 0 1 1 1 0 1 1 1 0 -
Bit Masking – Toggle a bit on/off ▪ Take the
following binary number and we want to toggle the bit indicated: 0010 -
Bit Masking – Toggle a bit on/off ▪ Take the
following binary number and we want to toggle the bit indicated: 0010 0001 ▪ Introduce our new number and bit shift it so it lines up with the one we want to toggle then perform an XOR operation. -
Bit Masking – Toggle a bit on/off ▪ The original
bit was ON, and after an XOR with a 1 it is now OFF. 0010 0010 XOR 0000 -
Bit Masking – Switching a bit off ▪ To turn
a singe bit off we need to do the following operation: x AND 0 = 0 A B Result 0 0 0 0 1 0 1 0 0 1 1 1 -
Bit Masking – Switching a bit off ▪ However we
can’t use the same procedure as before with a 1 bit shifted as it would give us the wrong answer as we can see below: 10011101 00001000 AND 00001000 -
Bit Masking – Switching a bit off ▪ What we
need to do first is NOT our new value BEFORE we perform the logical AND. 10011101 11110111 AND 10010101 ▪ Our new number is the same as the original with the bit switched OFF. -
What if we just want to query a bit? ▪
Sometimes we may just need to know what a bit value is but not write any changes to the original number: ▪ To do this we introduce a new number as before, bit shift to the bit we want to query and then perform and AND operation ▪ If the number we get back is all zeros, then the bit is a zero. ▪ If the number we get back contains a 1, then the bit is a 1. 10101110 00001000 AND 00001000 -
Bit Masking – Task ▪ Showing your steps as explained,
switch the highlighted bits in the following numbers ON: 10101000 10010001 10011111 11110111 -
Bit Masking – Task ▪ Showing your steps as explained,
switch the highlighted bits in the following numbers OFF: 11101001 10011011 10100101 11000101 -
Bit Masking – Task ▪ Showing your steps as explained,
toggle the highlighted bits in the following numbers: 11011011 10001011 10011001 10101010 -
So where is bit masking used? ▪ Bit masking can
be used in image editing. -
So where is bit masking used? ▪ Bit masking can
be used in image editing. -
So where is bit masking used? ▪ Bit masking can
be used in image editing. -
So where is bit masking used? ▪ Bit masking can
be used in image editing. -
So where is bit masking used? ▪ Bit masking can
be used in image editing.