Binary masks are used for various purposes but only one reason. Binary masks are used to change specific bits in the original value to the desired setting(s) or to create a specific output value. A binary mask is used to change one or more bits from 1 to 0 or vice versa using a boolean comparison operation

Places Binary Masks are used

  • Network Masks
  • Computer Graphics
  • Changes to CPU Registers & Flags

NETWORK MASKS

This ability to change the values of individual bits is useful in the area of networking where a binary mask can be used to by 'mask out' the network portion of an Internet Protocol Address leaving the host portion of the IP Address which is the address the computer wants to talk to. A router will do it the other way and mask out the host portion of the address in order to identify the network to which it should route an IP packet. The binary ones in the network mask are used to preserve the network portion of the IP address. In the example below, 24 bits worth of ones are used in the mask. This designates the first 24 bits of the IP address as network bits.

This works as follows:

 DECIMAL EXAMPLE
  Network Portion Host Portion
IP ADDRESS 217 124 198 140
( ^ ) MASK 255 255 255 0
RESULT 217 124 198 0

 

 BINARY EXAMPLE (Same values as decimal above)
  Network Portion Host Portion
IP ADDRESS 11011001 01111100 11000110 10001100
( ^ ) MASK 11111111 11111111 11111111 00000000
RESULT 11011001 01111100 11000110 00000000

 

The two examples above represent exactly the same numerical values; the difference between the two tables is that the first table represents the IP address in decimal numbers, and the second table is in binary numbers. As you can see, when the IP address and mask are compared using the AND logical function, the result is that the host portion is 'masked' out, and all that is left is the network portion of the address. This is how networked computers determine if they are on the same network, or if the destination they are sending to is on a different network (and should therefore be sent to the default gateway).

OTHER MASKS

Other applications of masks are for changing flag settings in the processor's registers. Using either a logical OR function, or a logical AND function, a value as small as a single bit can be toggled to the opposite value ( zero to one or vice versa).

CHANGING ZERO TO ONE (Binary 'OR')

If you start with a zero and you need to set a specific bit position to one, use a logical OR with a mask of one in the bit position you wish to toggle. The remaining bits in your mask should be set to zero.


BINARY 'OR' OPERATION

Original Value 1 1 1 1 1 0 1 1
Binary Mask 0 0 0 0 0 1 0 0
Result of 'OR' operation 1 1 1 1 1 1 1 1

 

CHANGING ONE TO ZERO (Binary 'AND')

If you need to toggle a specific bit from one to zero, use a logical AND with a zero in the position you wish to toggle. The remaining bits in your mask should be set to one.

BINARY 'AND' OPERATION

Original Value 1 1 1 1 1 1 1 1
Binary Mask 1 1 1 1 1 0 1 1
Result of 'AND' operation 1 1 1 1 1 0 1 1

 

 

<<  <  1 | 2 | 3 | 4 | 5 | 6 | 7  >  >>

 


Bookmark this page and SHARE:  

Search

Support InetDaemon.Com

Get Tutorials in your INBOX!

Free Training