As a continuation of my posts about strange language things, consider the following:
if (-3 == ~2)
Console.WriteLine("equal");
else
Console.WriteLine("not equal");
This prints “equal”.
Why? Let’s look at the binary representation:
0 0 0 0 0 0 1 0 = 2
1 1 1 1 1 1 0 1 = -3
The ~ operator will flip the bits, and therefor the 2 become equal.
Posted: December 22nd, 2010 | Author: benlakey | Filed under: benlakey.com | Tags: C# | No Comments »
A number of people have been asking me about the strange bits of code in my previous blog post, so I felt I should explain a bit.
The Javascript
The reason why this…
… gives you 53 is because javascript’s concatentation operator is the plus sign, and when it sees a string on the left operand, it forces the right operand to be promoted to a string, and then performs concatenation.
The precise opposite occurs when the operator is the minus sign.
The operator minus is the mathematical subtraction operator, and so when it looks at the operands, it promotes both sides to a numeric type, and performs the subtraction.
The Java
So why on earth does Java think this is OK ?
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
This is because the act of doing the assignments at the top sets the underlying primitive int to be of value 1000, but the actual variables themselves are of type Integer, which is an object to wrap the int. Therefor, when you do a numeric comparison with either >= or <= it will use the primitive type underneath, whereas when you use the == operator, it tries to compare the objects, which are 2 different instantiations of Integer objects, and are therefor not equal.
Hope this helps.
Posted: December 15th, 2010 | Author: benlakey | Filed under: Uncategorized | Tags: Java, Javascript | No Comments »
Did you know that in JavaScript, if you do this:
You will get 53.
But yet if you do this:
You will get 2.
That’s what happens when you have a weakly typed language using ‘+’ for string concatentation. :)
Another oddity… In Java:
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
I love these little language quirks. :)
Posted: December 6th, 2010 | Author: benlakey | Filed under: benlakey.com | Tags: Java, Javascript, wtf | No Comments »
We programmers keep piling up these leaky abstractions, shoring up as best we can, desperately attempting to stay ahead of the endlessly rising waters of complexity.
Source: Coding Horror Jun 30, 2009
Posted: December 5th, 2010 | Author: benlakey | Filed under: benlakey.com | Tags: abstraction, LINQ, software engineering | No Comments »
There is a lot of overlap between LINQ to SQL and LINQ to Entity Framework; the line is pretty blurred. I did some reading and it looks like LINQ to SQL and LINQ to Entity Framework were being developed around the same time, and the end result was strikingly similar. Microsoft however is now throwing their weight behind LINQ to Entity Framework as the future of relational database access, so it’s probably a smart move to go that route. Here’s a blog post from Tim Mallalieu made around PDC 2008, which doesn’t outright say LINQ to SQL is dead, but most can probably read between the lines.
Update On Linq to SQL And LINQ to Entities Roadmap – Tim Mallalieu
I should point out that StackOverflow.com, the popular Q&A site for programmers, is leveraging LINQ to SQL. (And presumably the entire StackExchange network).
Posted: December 5th, 2010 | Author: benlakey | Filed under: benlakey.com | Tags: C#, Entity Framework, LINQ, Microsoft, ORM | No Comments »
As I tend to do from time to time; Here is another good set of interview questions to toy with (These particular ones are in C#):
private static char FindLongestRun(string str)
{
Dictionary<char, int> runs = new Dictionary<char, int>();
char last = str[0];
runs.Add(last, 1);
for(int i = 1; i < str.Length; i++)
{
if (last == str[i])
{
int oldCount = runs[last];
runs.Remove(last);
runs.Add(last, oldCount + 1);
}
else
{
runs.Remove(str[i]);
runs.Add(str[i], 1);
}
}
char longest = last;
foreach (char c in runs.Keys)
{
if (runs[c] >= runs[longest])
longest = c;
}
return longest;
}
private static int BinSearch(int[] arr, int seek)
{
return BinSearch(arr, 0, arr.Length - 1, seek);
}
private static int BinSearch(int[] arr, int min, int max, int seek)
{
if (min > max)
return -1;
else if (min == max)
return (arr[min] == seek ? min : -1);
else
{
int partIdx = min + ((max - min) / 2);
int left = BinSearch(arr, min, partIdx, seek);
int right = BinSearch(arr, partIdx + 1, max, seek);
if (left != -1)
return left;
if (right != -1)
return right;
return -1;
}
}
private static int Atoi(string number)
{
int result = 0;
int multiplier = 1;
for (int idx = number.Length - 1; idx >= 0; idx--)
{
char digitChar = number[idx];
int digitValue = digitChar - '0';
result += (digitValue * multiplier);
multiplier *= 10;
}
return result;
}
private static string ReverseString(string str)
{
char[] strChars = str.ToCharArray();
for (int idx = 0; idx < strChars.Length / 2; idx++)
{
char tmp = strChars[str.Length - 1 - idx];
strChars[str.Length - 1 - idx] = strChars[idx];
strChars[idx] = tmp;
}
return new String(strChars);
}
private static int BitCount(int number)
{
int bits = 0;
while (number > 0)
{
bits += (number % 2);
number /= 2;
}
return bits;
}
Posted: December 3rd, 2010 | Author: benlakey | Filed under: benlakey.com | Tags: C#, data structures, interview | 2 Comments »