Table of Contents
- Golang Network example - XOR ^ - Subnet Mask
- Golang Network example - Shift Left << - Building Networks
- Golang Network example - Shift Right >> - Building Networks
This article was made for a youtube video, if you want to check that out: https://www.youtube.com/watch?v=tiTx1IenvaA
Golang Network example - XOR ^ - Subnet Mask
The XOR operator, also called exclusive OR or exclusive disjunction, or the difference operator, is used in plenty of examples in networking. XOR is commonly used in network programming for:
-
Error detection (checksums)
-
Simple data obfuscation
-
Network packet manipulation
In this video we will see code examples on how a simple data obfuscation works and how to find different bytes on a sent package using XOR.
In essence, when the bits are equal, the result will be 0 (false).
One important thing to notice is that in Go, XOR operator applies only to integers. In other words, it compares bits of two integers, and for each bit position the result will be 1 only if the bits are different and 0 if they are the same.
Let’s first understand how the XOR operator works with a simple git analogy. Imagine you’re tracking changes between your branch and main.
-
If neither branch changed a file (0 ^ 0), no conflict (0)
-
If your branch changed it (1 ^ 0), there’s a difference (1)
-
If main branch changed it (0 ^ 1), there’s a difference (1)
-
If both branches changed the same file (1 ^ 1), changes cancel out for comparison (0)
In our practical example, we will see how we can obfuscate data packages simply using the XOR operator and a KEY.
XOR can be used to do simple data obfuscation because it’s reversible, when you XOR data with a key and then XOR it again with the same key, you get the original data.
Another great example about this operator is, we can quickly check if different IP addresses are on the same subnet. As we saw in part 1 of this video series, the subnet mask can easily tell us if a device is or not on the same network or if it needs to be routed through the gateway, it’s a smart way of doing quick routing on network programming. Let’s see how.
This is where Bitwise XOR and AND operators come in. The device uses XOR to compare its own IP address with the destination address. The result is then compared with the subnet mask using the AND operator to check if the destination is in the same subnet or not.
Golang Network example - Shift Left << - Building Networks
Let’s now talk about one of the most used bitwise operators, the shift left. Think about it like moving all the bits to the left and filling all empty spaces with zeros.
Essentially, each shift left is multiplying the number by 2.
So using the shift left operator is a really quick way of performing mathematical operations. But why is this useful?
Let’s find how we can find the Network Size, or in simpler words, how many hosts we can have inside a subnet.
Let’s first understand how this works. An IP address has 32 bits, to correctly create a network, we need to specify the CIDR range, which will tell the amount of devices the network will have.
If we specify a CIDR of 24, subtracting 24 from 32 bits, which is 4 bits, and in 4 bits we can have 16 different devices (hosts).
The smaller the CIDR number, the more hosts the network can have. Of course this comes with a cost.
Now, we can easily calculate the network size using the left shift operator in go, as it can quickly perform operations to the power of 2.
Golang Network example - Shift Right >> - Building Networks
The shift right operator is another one that is most used on programming networks.. Think of shift right like moving all the bits to the right and throwing away the bits that fall off.
There are plenty of ways to use the shift right operator in network programming.
Let’s see visually how it works.
So essentially, the shift right operation is dividing by 2 for each integer unit you have:
Now, let’s see a simple example where we can extract all octets from an IP address using the shift right operator.
That’s it for today. Thanks!