July 01, 2016

The mad hat operator

Some stuff about the hat ^ operator. When I looked at this `buggar` I realised I hadn't used or seen this for many many years, which when you do a test of some sort is pretty bad. Because you look kinda lame when you get basic questions like this all wrong.

It's not that I hadn't used the xor operator I just never really took the time off to dwell into the operator since my time with boolean logic. So, I decided to work a little on the mad ^. In most languages like c/c++/java/python/bash and several others ^ means bitwise xor aka exclusive or. But in computer math ^ also means power of. The power off operator is the c function known as pow() (see man pow) for details.

Looking at a few simple examples: 10^9, 2^4, 5^7, 4^5, 3^3 and 5^0 seems to have simple answers.

10^9 = 1.000.000.000
2^4 = 128
5^7 = 78125
4^5 = 1024
3^3 = 27
5^0 = 1

All of the above are plain math answers. Answers that should be part of any ones math vocabulary. However, I'm not sure how the theory behind the 3^0 og 5^0 equations both equal 1 actually is. There's a theory on wikipedia that is really scientific and explains the details, there's a more fun description here however none of the articles say why x^0 is 1 they just claim it is? So why is it 1? Right about here I can hear all my previous teachers scream because it is.

I guess the answer to the above equations are: "It depends". Weather you mean in math or using binary operators. Since if you type the expression into either of the equations into any of the languages mentioned, you'll get completely different answers:

10^9 = 3
2^4 = 6
5^7 = 2
4^5 = 1
3^3 = 0
5^0 = 5

The cool statement of the math power off is that 2^1 or as it is stated x^1 = x. But in binary the answer is that it depends. Since: 2^1 = 3 and 3^1 = 2. Why is this, well the xor operator looks at the numbers binary and only lets values through that are not the same. Binary 2 and binary 1 are: 10 and 01 respectively. When you xor these you get:

10
01
--
11

11 is binary for 3. Xor 'promotes' or lets only ones through. Fine but why is 3^1 =2 then. Same simple answer:

11
01
--
10

Because any similar numbers is set to zero. So 0^0 = 0 and 1^1 = 0. Somehow I guess I have to accept the 2^0 math explanation since I seem to understand how the binary thing works. But anything times 0 is zero, execpt x^0. But can we prove it? Mathforum.org I guess we can:

x^0 = x^(1-1) = x^1 / x^1 = x/x = 1

Aka the law of the exponents. The important thing for me is that the reason I didn't know is because I never needed to know for dealing with pratical problems. But now I did find mathforum.org in the process guess I should read it now. Here's a small program madhat.c that plays with the ^ operator. Remember the answer, for this specific operator on how various equations turn out, is: "it depends".

No comments: