Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Thursday, October 19, 2023

How to Count and Convert Numbers

Regardless of how old you are, there is a chance you do not actually know how to count correctly. I decided I would post my guide to proper counting. The reason I say you might not know how to count is because counting in anything should be universal. Whether it is base 10, what we use on a day to day, base 8, base 2, or even base 16, it all works the same. So to prove it, here is my counting method. So, let's begin.

When you count, you probably go one, two, three, four, five, six, seven, eight, nine, ten. When working with real numbers, that works fine. However, this undermines what is actually happening. We use "decimal" counting. This means base 10. But what does that really mean? The base refers to the number of possible values. The values are zero through nine. The number ten is actually not a unique value. It is two values. That is a 1 in the second place and a 0 in the first. Each number you see individually is a value in a specific spot. To show this further, let us take the number 1234. You have a 1 in the fourth place, 2 in the third, 3 in the second, and 4 in the first. Using this same model and some math, we can draw this out to there you get a basic equation where the individual number can be represented relative to the base numbering and its place. It would look like base^place*value. We can then get the actual number by obtaining the sum. So let's expand this further for the number 1234. To do so, we first need to establish what the place value is. The place value starts at 0, because we will take advantage of some special math properties. Thus the number 1234 would be expanded with the base being a constant, in this case 10.

(10^3*1) + (10^2*2) + (10^1*3) + (10^0*4)

The special rule to remember is anything to the power of zero is always one. We have 4 places, starting at zero and ending in 3.

(1000*1) + (100*2) + (10*3) + (1*4)

Hopefully at this point, you can now see the relation of the place with the base. Now we put in the values.

1000 + 200 + 30 + 4

With that we get 1234 as our total value. It may seem super convoluted, but this is the realistic way numbers work. Understanding this, we can use the exact same method to convert any number from any base to a number we humans of base 10 calculations can understand. So let's show the relation to binary, or base 2. Picking an arbitrary binary value, 10011010, let us calculate what that is in a number we can understand. To start out with, we have 8 place values, and a base of two. That should let us drop in all the information nice and easy.

(2^7*1) + (2^6*0) + (2^5*0) + (2^4*1) + (2^3*1) + (2^2*0) + (2^1*1) + (2^0*0)

There are ways to shorten it, which if you can't see you should momentarily, but I will go through all the steps to actually show what it all looks like. So the next step is as follows:

(128 * 1) + (64 * 0) + (32 * 0) + (16 * 1) + (8 * 1) + (4 * 0) + (2 * 1) + (1 * 0)

Since anything times zero would be zero, you could have technically just thrown out those sections from the equation, but we will continue on with them in there.

128 + 0 + 0 + 16 + 8 + 0 + 2 + 0

This then means our binary value was the number 154. Just to show it works for any base number, let's do an octal number. Since it is octal, that means the numbers can be anything with values 0 to 7. Let's try 651.

(8^2*6) + (8^1*5) + (8^0*1)
(64*6) + (8*5) + (1*1)
384 + 40 + 1
425

While the math itself may be hard, getting the equation makes it easy to just figure it out with any old calculator. Working with hexadecimal works a little differently, as we need to do some extra translation. Hexadecimal is base 16, which means the values are 0 to 15. Since we have only ten numbers, 0 to 9, we need to create more numbers. For this, we use letters. The letter values are as follows.
A=10
B=11
C=12
D=13
E=14
F=15

With this extra bit, we can translate a hexadecimal number to an understandable number. Let's use the number 1C3A. The numbers here will be much larger, but as long as we have the equations down, it should be easy.

(16^3*1) + (16^2*C) + (16^1*3) + (16^0*A)

Now, let's substitute any letters and we should be able to roll on through it.
(16^3*1) + (16^2*12) + (16^1*3) + (16^0*10)
(4096*1) + (256*12) + (16*3) + (1 * 10)
4096 + 3072 + 48 + 10
7226

As you can see, we can re-use the same pattern and adapt to anything we need. So long as we keep track of all the information necessary, it's easy enough. So to reiterate, the base is a constant for the whole equation. Each value number is actually two pieces of information, place and value. Common bases for number systems are binary (2), octal (8), decimal (10), and hexadecimal (16). Even so, you can pick any base you want, even lucky 13.

Now, how do we reverse it? The method is a little more obscure, however it is still doable. While the method mentioned before is not sensitive to the order since it is a sum, going backwards is. The good news is, we are worried about the same information of base, place, and value. So let's pick a number we did before, 154, and convert that back to binary. The way we do that is divide by the base and use remainder for the value and the quotient for the next step. We work our way through more or less as (total/base) -> 1 if remainder else 0 -> (quotient/base) ...

A bit more confusing, but let me show how that goes.
154/2 = 77 -> 0

Now the quotient is 77.
77/2 = 38.4 -> 1

The quotient is 38, remainder is 4, so we have a 1.
38/2 = 19 -> 0
19/2 = 9.5 -> 1
9/2 = 4.5 -> 1
4/2 = 2 -> 0
2/2 = 1 -> 0
1/2 = 1.5 -> 1

Now we take all of what is at the end and reverse it to get 10011010, what we started with. Numbers other than binary are a little easier if you know the modulo operator. With an equation, it would be more along the lines of using modulo(%) and integer division for the quotient to look like (value % base) -> (quotient % base)... Making this a little easier with a calculator. However, the structure is the same. So let's use 425 and go back to octal.

425/8 = 53.125 -> 0.125 = 1/8 giving us a modulo 1
53/8 = 6.625 -> 0.625 = 5/8 giving us a modulo 5
6/8 = 0.75 -> 6/8 giving us a modulo of 6

We take that and reverse the order and you get the original we started with in octal, 651. Now let's tackle the one last number, 7224, and get it back to the original hexadecimal. As before it will require the extra step of substitution to get the letters in there. Let's roll through this.

7224/16 = 451.625 -> 0.625 = 10/16 gives us a modulo 10 which is A
451/16 = 28.1875 -> 0.1875 = 3/16 gives us a modulo of 3
26/16 = 1.75 -> 0.75 = 3/4 = 12/16 gives us a modulo 12 which is C
1/16 = 1 gives us a modulo 1

Now we take those values, run them backwards and we get 1C3A. With that, you can hopefully see that converting around different base numbers is actually a simple process and hopefully reveals enough information to truly understand what counting is really representative of.

I also want to talk about some bonus information. Number Order. So the way numbers get processed can be in relation to either the smallest to largest value, which is called little endian, or largest to smallest, called big endian. When you get deep into computer programming or number processing, this becomes very important as to preserving the significant figures we want to be concerned with. As I mentioned before, since converting numbers into decimal was a sum that the order did not matter. That means you can process the numbers in reverse order similar, just like when you convert them off into other bases.

That's all there is to it! With any old calculator you should now be able to do conversions of binary, hex, octal, or whatever else you fancy. Hope it helps someone.

Thursday, September 3, 2020

Starting Out as a Computer Tech (part 3)

 For this part, I would like to focus on basic networking. In IT lines are often blurred and one thing I noticed with a lot of new techs is they know little to nothing about networking and yet are expected to be almost entry-level network specialists. Luckily for myself, networking is something I actually really love working with (and due to some past things revolves around a potential Techs from the Crypt story). So let me shine some light on networking, terms, and basic troubleshooting.


First, let's go over some terms you absolutely need to know.

IP Address - Internet Protocol Address

Subnet - A block of IP addresses

IPv4 - IP version 4, has four octets (8 bits) that are shown in decimal between 0 and 255

IPv6 - IP version 6, 128 bit address broken down into 8 groups of 16 (called hextets or hexadectets) shown in hexadecimal (0 through 9 and A through F), more often than not you'll just be turning it off

OSI (model) - Open Source Interconnection model, has 7 layers

Layer 2 - Layer 2 of the OSI model, information is called frames, commonly what switches use, what the MAC address is for

Layer 3 - Layer 3 of the OSI model, routing level, information is called packets, uses IP addresses and subnets

MAC address - Also called a hardware address, given to the network device on the chip itself, written in 6 octets in hexadecimal (0 through 9, A through F)

Protocol - A term to explain a standard being used

TCP - Stateful communication that makes sure all information is delivered like viewing a website

UDP - Stateless communication that does not verify information completed its journey, used for things like phones and DHCP

DHCP - Dynamic Host Control Protocol uses a server to assign an IP address to a device

DNS/Name Server - Domain Name Service used to turn something like www.google.com to an IP address for routing traffic

Gateway - IP address of the device you use to get to the Internet

APIPA - Automatic Private IP Addressing is a default IP you get when no IP is set or DHCP server reachable

Static IP - An IP you manually set for a device

WAP or AP - Wireless Access Point or Access Point used for wireless internet connection

Private IP - An IP address made to be used within an organization

Public IP - Live on the Internet IP addresses

NIC - Network Interface Card

Ethernet - standard copper cable to plug into your network

2.4G - 2.4 Gigahertz wireless band for wifi, it's old

5G - 5 Gigahertz wireless band for wifi


You should be able to recognize those terms even if you are not clear on how they work, just know what they are and hopefully we can fill in enough blanks to make something coherent. There are a few more terms, but those will be introduced in context since they can technically mean different things. So let's start piecing this together.


You have a computer with a NIC for network access. You plug an Ethernet cord into the NIC (they have different standards like Cat5, Cat5e, Cat6, Cat6e, Cat7, you get the idea) and the other end goes into a connection on the wall. The plug on the computer is an Ethernet port, the plug on the wall is a jack, probably a keystone jack or may be called a wall port (really depends on who you're talking to). From that wall, it will go to something called an IDF (Intermediate Distribution Frame) or MDF (Main Distribution Frame), also called a Networking Closet. In that closet will contain things like switches and/or routers. Hubs are old and I would hope you are not using them.


So now that the computer is hooked up, it can try to communicate with the network to get information it needs to properly use it. The computer will send out a DHCP Broadcast to find a DHCP server. If one cannot be found or one is and cannot provide an IP address, the computer will assume an  APIPA address and that ends it. You can tell an APIPA because the address will start with 169 (more on how to see that later). If a DHCP server responds, they communicate and an IP address, along with DNS and Gateway info, will be passed to the computer.  To communicate the DHCP information, the MAC address is used.

 

Let's make a scenario. You are in a meeting and people are talking about putting a computer lab into a subnet. On the notes you see 172.16.3.0/24. What does this mean?

The 172.16.3.0 is the network and the /24 is the subnet. The subnet may also be written as 255.255.255.0. Information we can extract from this would be that there are 256 IP address available within that block, but technically only 254, or 253 if you exclude a gateway. We can get to this conclusion easiest with the / notation and a calculator:

2^(32-n) where n=subnet


This is because an IPv4 address is made of 32 bits broken into 4 octets. Each octet can be 0-255 for a total of 256 values or 2^8. The reason for the other subnet notation is that is the representative value it gets ANDed to. At this point we are pretty far above and beyond what you need to know, so here's the simple conversions:

/8 = 255.0.0.0

/16 = 255.255.0.0

/24 = 255.255.255.0

So if we need to take an add subnet like 18 and want to show it in the other notation, we can start with the first two octets, or the /16:

255.255.0.0

Now we can see that we have two left over (18-16). So let's look at that octet in binary real quick...

11000000

We know this because there are 8 bits in an octet, we have two bits, so we replace the 0s starting on the front end. So two ones, means we have six zeroes left over. Now we can break out a calculator to do some conversions. So convert binary to decimal, you need to count to what place the 1 is in, so we have a 1 in the eighth spot and a 1 in the seventh spot. Binary is base 2, so we use the number base 2. Out equation therefore is:

2^7 + 2^6 = 192

I know what you're saying, wouldn't it be 2^8 + 2^7? No, this is computers. We start counting at 0. Here's the proof why:

00000001 = 1 = 2^0

If you are completely lost, it's okay. You shouldn't need to do this, but I really wanted a reason to do some complicated looking math. The end result is your subnet would be as shown:

255.255.192.0


If you want to practice or figure these things out faster you can do two possible things. Pick a number between 0 and 32 and convert so you can get the actual math down AND/OR memorize the values of each binary bit along a line of 8 and add them or learn them already added. I'm personally a fan of learning the math because then you just have that to remember and apply to many things rather than single purpose memorization... 1, 2, 4, 8, 16, 32, 64, 128. In interviews I have been asked to do these things in my head, and personally I always get away with rattling off the math and some explanations of what to exclude and it gets me by.


I suppose I should mention why we get rid of 2 addresses off the bat. We get rid of the binary all 0s, like 172.16.3.0 because that's the network address and we get rid of the all 1s because it is the "broadcast" address. Then often there is an implied gateway, but do not assume that is always the case. You can have a network with no gateway, it is not a requirement. The gateway is often the first binary 1, so in the example's case it would be 172.16.3.1, 172.16.3.0 is the network, and 172.16.3.255 is the broadcast.


With that headache out of the way, let's talk about the easy stuff, how to view your network settings. There are a few ways to view your IP information, both GUI and CLI. Starting with GUI you can go to the start menu cog Settings, Network category, and click on the "Change connection properties." This should give you the IP, DNS servers, and MAC. This is good in a pinch, but the CLI options give more information. So first, the Powershell commands. The Get-NetIPConfiguration is a good way to get a lot of information. There is also Get-NetIPAddress, which gives information on some other interfaces you may not see that use the network stack. There is also Get-NetAdapter that will give you the MAC address, just compare it to Get-NetIPConfiguration. Finally there is the CMD command ipconfig /all. Pick whichever method is your favorite.


After you view your network settings, you should have all the information available to set a static IP. When setting a static IP, you can optionally set a gateway and DNS server(s). As usual, there is a GUI and a CLI way to do this. For the GUI we go to the start menu cog Settings, then the Network and Internet category. On the left-hand side you click on Ethernet, then Change Adapter Options. On the interface you want to set the IP, right-click it and hit properties. In the middle of that window you will see Internet Protocol version 4, select that then hit the properties button. With the first radio button, select Use the following IP and enter in the necessary information. Often times I end up just using the same gateway and DNS that DHCP had set and put in a static IP and subnet.


To set a static IP using powershell, you first want to get your interface index from either Get-NetIPConfiguration or Get-NetIPAddress. Get any other information you may need from those results as well. Then we can set every option necessary with the New-IPAddress command and Set-DNSClientServerAddress command. So it could look something like this:

New-NetIPAddress -InterfaceIndex 20 -IPAddress 192.168.1.30 -DefaultGateway 192.168.1.1 -AddressFamily IPv4 -PrefixLength 24

Set-DnsClientServerAddress -InterfaceIndex 20 -ServerAddresses 192.168.1.5,8.8.8.8 


I should take a moment to mention that if you need DNS on the Internet should a DNS server drop, Google has two you can use that are easy to remember. They are 8.8.8.8 and 4.4.4.4. It will not help with your intranet, but it is a good fallback for general Internet stuff.


Now let's talk about wireless, the wonderful world of wifi. While I doubt I have to tell you how to add a wifi network, I feel I need to mention how to remove a network to then re-add it. It actually can solve a lot of random wifi issues. So go to the start menu cog Settings, Network and Internet category, then Wi-Fi on the left. From there, select "Manage known networks." On the next page, you should be able to just click on any of the networks and hit "Forget" to remove them. On the same topic of Wi-Fi, your 2.4G bands reach farther than 5G, however they are more prone to interference especially from things like microwaves. Walls can also hinder the wifi signal and approaching from an angle means there is more material to go through, slowing it down even more. 2.4G cannot handle as many clients or as high a speed as 5G.


Moving on to the subject of Network hardware, typical topography of a network will be something along the lines of a router connected to switches connected to devices. Switches operate on Layer 2, so they are concerned only with the MAC address of a device. A Layer 3 switch is a switch that can operate on Layer 3 to some degree, like a managed switch with a web interface. A router operates on Layer 3 and is concerned with IP address and subnet. Each jack on a switch or router is called a port, or a physical port. On a device it is also called an interface. Getting any more in depth is a massive topic all on its own.


The problem with "network" trouble is that it can encompass such a wide variety of stuff that there is a lot to consider. Generally as an IT Specialist or the like, it will be your job to determine what the problem is to then see if there is a path of escalation to someone else or if you can deal with it. Problems can include wifi connection trouble, lag, website errors, network share problems, network printer problems, or general web issues. I will try to demonstrate using past experiences.


"A website is blocked or something, I get an error and cannot go past"

This is what I often get when there is a security certificate error. I hear this from the inept to the savvy. This is an easy fix, there will be an "Advanced" option that leads to a "proceed anyway" option.


"I can see the wifi network, but it won't connect"

This is usually as easy as removing the wifi network and re-adding it.


"Everything is laggy and slow"

This is an infamous work order I get all the time. If you run the Get-NetAdapter command on Powershell, you can see what the speed should be currently negotiated. Usually you can pass that information along with what physical port on a network device the device is plugged in to and pass it along to a Networking specialist. There is a chance if the speed is slower than it should be that there is a cabling issue or a port going bad.


"This website I NEED isn't working! HELP!!!111"

Often a lot of drama around work orders like this and it's usually something like a 500 error on a website. If you are getting an error on the website like 500, 502, 503, 404, etc. it means that the network is fine and it's in the hands of whoever controls the server.


The rest are usually wifi issues because it's wifi. Unfortunately, wifi is just not as reliable as a good old fashioned hard line.


That's pretty much way more than you need to usually know about networks starting out as a Computer Tech. Hopefully now you can go in at least able to grasp what veteran techs are trying to say. Unfortunately, most techs forget what it was like starting out when you never really expect to need to know networking because there's a whole job line dedicated to it. Hope this helps!

Thursday, July 27, 2017

Old Tech: Powerbook 520C

So I found this amazing piece of technology and decided to see if it still worked... and it did! It's from 1994, which means I was four years old when someone bought this. The battery has long since swollen, but the rest appears to be in great condition. Although, there is some fading on the bottom left corner.


Check it out, there's a floppy drive! I knew I kept my floppy disks around for a reason.


I can identify a SCSI, headphone, and microphone port, I assume the one left to the SCSI is some kind of a display port, but this is something I think I need a manual for.


A nice swollen battery.


Some stickers for accessibility I assume. Regardless, this thing looks cool.


So you remember when the Apple logo was a rainbow? I do. I was a little kid at the time and disliked those computers being called Macintosh because I don't like mcintosh apples. I prefer golden delicious or honey crisp.


And here is video proof it works! Enjoy the background talk. I wanted to include all the wonderful noises and clicks from the good old days.



I hope someone enjoys this. I did, seeing stuff almost as old as I am in action is interesting to me.

Wednesday, February 20, 2013

Fibonacci Generator

I was reading up on various types of morphism recently. While I was disappointed in the examples, as they were showing off bad practice, even if they were just an example, I got an idea. While I do have a post showing off a similar bad example with calculating a Fibonacci number, I have since refined it into a faster and more efficient method and an explanation to it you can find here.

My main focus of interest was looking at catamorphism and anamorphism. In the process of reading up, I came across other types of morphisms that while were mathematically interesting, I only saw bad programming being shown emerging from the ideas.

To explain in the simplest way possible, catamorphism is a generator and the opposite of anamorphism, which is a fold. Generators are functions that produce data on-call, using the generated data to provide more data upon the next call. This can create an infinite data type or have an end. Counting can be catamorphic in nature. For the explanation, let's look at a Python generator.

def counting(x):
    while 1:
        yield x
        x += 1

This is the equivalent to this Haskell code.

counting x = [x..]

The Python code has a bit of a more robust description to it, but the idea is the same for both, they will count from x on up until forever. Anamorphism includes things like the sum function, a list of stuff goes in, and it's all folded together with a function (in the case of sum, adding) until one result remains.

Now to the cool part and what this all means. This all means that with the magic of generators, there is an easy way to create a fibonacci generator. This is a simple one-liner in Haskell and as far as I can tell, can outdo the crappy version of the recursive Fibonacci shown everywhere else.

fibgen a b = a : (fibgen b (a + b))

That's all there is to it. The idea is you supply the first two numbers and it does the rest, forever and ever. So if you want the numbers from it, just take them. Now I suppose I can be nice and give something similar in Python, although to be honest it will look pretty much like the counting function I made.

def fibgen(a, b):
    while 1:
        yield a
        a, b = b, a + b

With only some very small tweaking, I turned the counting function into a Fibonacci generator. Now, like magic, we have an easy way to cycle through a sequence of Fibonacci numbers. I want to try to work on various problems more in this manor for when list processing or other related activities that require sequential computations, I feel it simplifies the process and gives a more elegant solution (or if nothing else, it looks cool).

Thursday, September 20, 2012

Recursive Fibonacci

So after bashing my head against the wall working on Fibonacci recursively and thinking "why on earth is this so slow," I realized the solution was reducing everything to a lot of ones and adding them all up. While addition and basic arithmetic for a CPU is exceptionally fast, this was slow due to the magnitude, and would cause way too much recursion for it to even be considered a correct solution. So I searched the web figuring someone far smarter than myself would have not only realized this, but come up with a real solution for it. Sure enough, I found one. The solution they used was in C, so I found it a bit ugly as it required two function calls (so you can call it with one argument). So I converted it to Haskell.

Before posting, the short explanation is this. Fibonacci numbers are found by adding up the previous two numbers. This is not to say you should call it inappropriately like this.

fib :: Integer-> Integer
fib 0 = 0
fib 1 = 1
fib x = fib (x - 1) + fib (x - 2)

However, this is often the solution I see in tutorials about recursion and very legitimate sources. I feel the reasoning for this is to use only one function call and as little code as possible to make an "elegant", or at least readable solution. The solution I found, while it is not the prettiest thing, it is far more efficient and very fast and after taking some time to really read it, makes far more sense true to the derivation of Fibonacci numbers. So here is the solution I found and converted to Haskell.

fib :: Integer-> Integer
fib 0 = 0
fib n =
    let
        fib2 1 _ n2 = n2
        fib2 x n1 n2 = fib2 (x - 1) n2 (n1 + n2)
    in
        fib2 n 0 1

Credit to the solution here. Glad to see people finding valid recursive solutions and letting people like myself know there's a better way.

Sunday, April 29, 2012

2D Rectangular Collison Detection

I was just looking through some old code and came across something I couldn't quite work out in my head but had to find. Circles was very self explanatory, but rectangular shapes are a bit more awkward, so here's a simple way that will return either a true or false based on the idea that 0 is false and a number greater than 0 is true, assuming rectangle 1 is made of points a and b and rectangle 2 is made of points c and d.
Points relative to rectangle (showing collision)
We also assume each point is a list of the [x, y].

int checkCollision(int a[], int b[], int c[], int d[]) {
    int c1, c2, c3, c4, c5;
    c1 = a[0] < d[0];
    c2 = a[1] < d[1];
    c3 = b[0] > c[0];
    c4 = b[1] > c[1];
    c5 = c1 && c2 && c3 && c4;
    return c5;
}

That would (more or less) be the gist of detecting if 2 rectangles collide by finding if there is an overlap. The basic idea is make sure all the points are in an area they could  be in if there was an overlap. Then by making sure all are in an area they can be in if there is an overlap, detects the overlap, since all must be in those areas for an overlap to exist. For any other shapes that are not square, you must use trigonometry, which I may get into some day down the road.