Sunday, January 31, 2016

Euclidean Algorithm for GCD

The Euclidean algorithm is a cool little way to find the GCD of two integers. 

Say you have 46 and 90...

Then 95 = 46 * 2 + 3
And 46 = 3 * 15 + 1
3 = 1 * 3 + 0

so the GCD of 95 and 46 is 1. Maybe not the best example, but I'm hungover so you can google it.

Friday, January 29, 2016

Memory And Hex

As you may know I'm taking a class in C and C++ this semester (Spring of 2016) and have found C to be the best language ever (revised 2017: it's great and all, but not THIS great). It's really fun because of it's simplicity, the lack of functionality and user interface makes it easy and demanding of creativity. 

I just started reading up on pointers and memory addresses when I got curious about just how memory addresses are stored in pointers. Try compiling and running the simple function below to see how memory addresses are stored as hex values.


Monday, January 25, 2016

Probably Handy

Since I'm taking a class in C now I think the table below that I found here will come in useful at some point...

PrecedenceOperatorDescriptionAssociativity
1++ --Suffix/postfix increment and decrementLeft-to-right
()Function call
[]Array subscripting
.Structure and union member access
->Structure and union member access through pointer
(type){list}Compound literal(C99)
2++ --Prefix increment and decrementRight-to-left
+ -Unary plus and minus
! ~Logical NOT and bitwise NOT
(type)Type cast
*Indirection (dereference)
&Address-of
sizeofSize-of
_AlignofAlignment requirement(C11)
3* / %Multiplication, division, and remainderLeft-to-right
4+ -Addition and subtraction
5<< >>Bitwise left shift and right shift
6< <=For relational operators < and ≤ respectively
> >=For relational operators > and ≥ respectively
7== !=For relational = and ≠ respectively
8&Bitwise AND
9^Bitwise XOR (exclusive or)
10|Bitwise OR (inclusive or)
11&&Logical AND
12||Logical OR
13[note 1]?:Ternary conditional[note 2]Right-to-Left
14=Simple assignment
+= -=Assignment by sum and difference
*= /= %=Assignment by product, quotient, and remainder
<<= >>=Assignment by bitwise left shift and right shift
&= ^= |=Assignment by bitwise AND, XOR, and OR
15,CommaLeft-to-right

Thursday, January 21, 2016

Two's Compliment

Today I'm reading about two's complement and the difference between signed and unsigned integers. In the near future I'm expecting to know a bunch of random stupid (and probably useful) info about ascii, utf, and hex representations of different characters... Starting by reading here.

Saturday, January 16, 2016

Calculator

Remember that shunting yard algorithm post? Well I turned it into a calculator demo on my website. I made this alongside the implementation of redux that I've been posting about.

check it out here

Tuesday, January 12, 2016

Javascript Objects

Often, Javascript objects are used as a hashtable with a string being the key value. I was curious if this is how they were implemented behind the scenes and found that I was clearly not the first one. Check out this quora question... 

I think it's pretty cool they're not done with hashtables in the V8 engine.

Monday, January 11, 2016

Instanceof String

I was playing with javascript error handling and understanding where to use try/catch today when I ran into an interesting problem. As part of my error handling I tried to include a type check on the key portion of a key-value pair, just to ensure that the key is actually a string and not null, undefined, numeric... 


That's when I noticed the following...


According to MDN (ctrl + f "Primitives and string objects") javascript handles string primitives differently from string objects. When creating a string with single or double quotes, or the global String object, javascript returns a primitive string. However, when creating a string with the new keyword, javascript returns a new string object. This makes sense when you read it, but if you're unaware or unfamiliar it could easily bite you. 

Finally, the documentation does say that "JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings". The problem is that Javascript only does this for String object methods, but instanceof is not a string object method. Instead, since instanceof is a global relational operator, the string created by quotes remains a primitive string and fails the instanceof String test.