<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>System.Blog.Martens.Ben : BrainTeaser</title><link>http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx</link><description>Tags: BrainTeaser</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Brain Teaser #3 Answer</title><link>http://blogs.msdn.com/ben/archive/2008/05/13/brain-teaser-3-answer.aspx</link><pubDate>Tue, 13 May 2008 18:06:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8445516</guid><dc:creator>benmartens</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ben/comments/8445516.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8445516</wfw:commentRss><description>&lt;p&gt;If you haven't read the question yet, you can &lt;a href="http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx" target="_blank"&gt;read it here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The answer for this lies in the C# specification. Specifically, read section 8.13. There are two different ways you can use a using statement and in this case we are interested in the second way. The key words from the spec are &amp;quot;in this case &lt;code&gt;ResourceType&lt;/code&gt; is implicitly the compile-time type of the &lt;code&gt;expression&lt;/code&gt;, and the &lt;code&gt;resource&lt;/code&gt; variable is inaccessible in, and invisible to, the embedded statement.&amp;quot;&lt;/p&gt;  &lt;p&gt;So effectively what the code in our question means is:&lt;/p&gt;  &lt;pre&gt;Foo x;
using (/*y =*/ x = new Foo())
{
} // this calls y.Dispose();&lt;/pre&gt;

&lt;p&gt;In this example, y is effectively the copy of x that is made so that x becomes inaccessible and invisible to the embedded statement. If Foo is a reference type, you don't really see the difference because x and y point to the same type. However, if Foo is a value type, it is actually making a copy of the value and you're dealing with a completely different memory location.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8445516" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item><item><title>Brain Teaser #3</title><link>http://blogs.msdn.com/ben/archive/2008/05/07/brain-teaser-3.aspx</link><pubDate>Wed, 07 May 2008 17:41:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8445429</guid><dc:creator>benmartens</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/ben/comments/8445429.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8445429</wfw:commentRss><description>&lt;p&gt;This brain teaser also comes courtesy of Simeon Cran.&lt;/p&gt;  &lt;p&gt;What will the following code output? When you think you know, copy the code into Foo.cs and run &amp;#8220;csc Foo.cs&amp;#8221; and then run &amp;#8220;Foo.exe&amp;#8221;. Did it output what you expected? The brain teaser is to come up with the correct explanation for why the program outputs what it does.&lt;/p&gt;  &lt;pre&gt;using System;

struct Foo : IDisposable
{
    bool disposed;
    
    public void Dispose()
    {
        disposed = true;
        Console.WriteLine(&amp;quot;Disposed&amp;quot;);
    }

    static void Main(string[] args)
    {
        Foo foo;
        using (foo = new Foo())
        {
        }
        Console.WriteLine(foo.disposed);
        using (foo = new Foo())
        {
            foo.Dispose();
        }
        Console.WriteLine(foo.disposed);
    }
}&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8445429" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item><item><title>Brain Teaser #2 Answer</title><link>http://blogs.msdn.com/ben/archive/2008/05/06/brain-teaser-2-answer.aspx</link><pubDate>Tue, 06 May 2008 19:46:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8445447</guid><dc:creator>benmartens</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ben/comments/8445447.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8445447</wfw:commentRss><description>&lt;p&gt;Have read the question yet? &lt;a href="http://blogs.msdn.com/ben/archive/2008/04/30/brain-teaser-2.aspx" target="_blank"&gt;Read it here&lt;/a&gt;.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The problem is pretty simple if you have 00111 (where all the bits after the first 1 are also 1&amp;#8217;s.) You shift right, run the NOT operator, and then AND that result with the original. For example:      &lt;pre&gt;	ORIGINAL = 00111 
	SHIFT RIGHT = 00011 
	NOT = 11100 
	AND with ORIGINAL = 00100&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;The question now becomes, how can I take a number like 001xxxxxx and make sure all those subsequent values are 1? Well if you take 001xxx, shift right 1 and OR, you&amp;#8217;ll have 0011xxx. Then shift right two and OR and you&amp;#8217;ll have 001111x. Make sense? Shift by 4, then 8, then 16, 32, 64 and you&amp;#8217;re done. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the code:&lt;/p&gt;

&lt;pre&gt;// Clears all the bits on the given value except 
// the leftmost bit that was set
static ulong KeepHighestSetBit(ulong value)
{
	value |= value&amp;gt;&amp;gt;1;
	value |= value&amp;gt;&amp;gt;2;
	value |= value&amp;gt;&amp;gt;4;

	value |= value&amp;gt;&amp;gt;8;
	value |= value&amp;gt;&amp;gt;16;
	value |= value&amp;gt;&amp;gt;32;
	return value &amp;amp; ~(value&amp;gt;&amp;gt;1);
}&lt;/pre&gt;

&lt;p&gt;Congrats to &lt;a href="http://weblogs.asp.net/rajbk" target="_blank"&gt;Raj Kaimal&lt;/a&gt; for figuring this out!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8445447" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item><item><title>Brain Teaser #2</title><link>http://blogs.msdn.com/ben/archive/2008/04/30/brain-teaser-2.aspx</link><pubDate>Thu, 01 May 2008 02:27:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8445386</guid><dc:creator>benmartens</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/ben/comments/8445386.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8445386</wfw:commentRss><description>&lt;p&gt;This brain teaser comes courtesy of a co-worker named Simeon Cran.&lt;/p&gt;  &lt;p&gt;Using C# and &lt;i&gt;no branches, and no method calls&lt;/i&gt;, &lt;i&gt;no allocations, and no unsafe code&lt;/i&gt;, write a method that takes a ulong and clears all the bits in it except the highest bit that was set. Use as few operations as possible.&lt;/p&gt;  &lt;p&gt;e.g.:    &lt;br /&gt;0 -&amp;gt; 0     &lt;br /&gt;1 -&amp;gt; 1     &lt;br /&gt;2 -&amp;gt; 2     &lt;br /&gt;3 -&amp;gt; 2     &lt;br /&gt;4 -&amp;gt; 4     &lt;br /&gt;5 -&amp;gt; 4     &lt;br /&gt;6 -&amp;gt; 4     &lt;br /&gt;7 -&amp;gt; 4     &lt;br /&gt;8 -&amp;gt; 8     &lt;br /&gt;9 -&amp;gt; 8     &lt;br /&gt;10 -&amp;gt; 8     &lt;br /&gt;&amp;#8230;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8445386" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item><item><title>Brain Teaser Answer</title><link>http://blogs.msdn.com/ben/archive/2008/03/11/brain-teaser-answer.aspx</link><pubDate>Wed, 12 Mar 2008 02:35:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8163141</guid><dc:creator>benmartens</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ben/comments/8163141.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8163141</wfw:commentRss><description>&lt;p&gt;There was a lot of extra information in that &lt;a href="http://blogs.msdn.com/ben/archive/2008/03/10/brain-teaser.aspx" target="_blank"&gt;brain teaser&lt;/a&gt;. All you really needed was these two sentences:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Clearly 41 runs * 1210 feet = 49610 feet. The watch incorrectly said that I had skied 20,100 but had the correct number of runs. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;So if I had looked after one run, the watch would have said 1210. After two runs it would have said 2420. After 10 it would have said 12100. Obviously that line of reasoning can&amp;#8217;t continue indefinitely because by the time we get to 41 runs, the watch isn&amp;#8217;t showing what we expect. So what happened?&lt;/p&gt;  &lt;p&gt;The number is stored as a 16 bit signed integer. The maximum 15 bit number is 32,767. After reaching that number, the counter rolled over to 0 and kept increasing. 32,767+20,100 = 52,867. That&amp;#8217;s close enough to the expected 49,610 to be reasonable. The difference can be accounted for by slight rises and falls in the run (it&amp;#8217;s not perfectly smooth all the way down) and a little barometric pressure variation throughout the day.&lt;/p&gt;  &lt;p&gt;That's my theory anyway. Until I check the watch just before 32,767 and then do it again and check just after 32,767, I won't be sure. It seems very reasonable though.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8163141" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item><item><title>Brain Teaser</title><link>http://blogs.msdn.com/ben/archive/2008/03/10/brain-teaser.aspx</link><pubDate>Tue, 11 Mar 2008 03:36:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8140698</guid><dc:creator>benmartens</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ben/comments/8140698.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben/commentrss.aspx?PostID=8140698</wfw:commentRss><description>&lt;p&gt;During a recent &lt;a href="http://studio711.com/cs/blogs/ben/archive/2008/03/10/12833.aspx"&gt;Hope on the Slopes&lt;/a&gt; 24-hour ACS skiing fundraiser, I used my watch to track vertical feet throughout the day. The watch isn't perfectly calibrated, but it's reasonably accurate. The watch counts a run any time you make a vertical change of at least 100 feet (so when you ride up the lift you are going up at least 100 feet, it counts that as a run.) We started skiing at 9:30am and didn&amp;#8217;t look at the watch until 7:30pm. I only skied on one lift and it had a vertical rise of 1210 feet. At that time I was surprised to see that I had skied 20,100 feet and 41 runs.&lt;/p&gt;  &lt;p&gt;Clearly 41 runs * 1210 feet = 49610 feet. The watch incorrectly said that I had skied 20,100 but had the correct number of runs. Huh?&lt;/p&gt;  &lt;p&gt;I had plenty of time to think this over while I was riding the lifts later that evening. Eventually I realized that the watch was actually tracking all of my vertical feet correctly. Do you know what happened?&lt;/p&gt;  &lt;p&gt;I'll make another post in a couple days with the answer. Leave a comment if you know the answer.&lt;/p&gt;  &lt;p&gt;These additional facts may or may not help you:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;I wore the watch the whole time. &lt;/li&gt;    &lt;li&gt;The watch was on the whole time (remember that it correctly counted all 41 runs.) &lt;/li&gt;    &lt;li&gt;There was no major weather event that changed the barometric pressure. &lt;/li&gt;    &lt;li&gt;It was accurately tracking my vertical feet the entire time. &lt;/li&gt;    &lt;li&gt;I didn't stop or restart the watch at any point (again, remember that it showed 41 runs.) &lt;/li&gt;    &lt;li&gt;The highest number I have ever seen it record previously is 28,000. &lt;/li&gt;    &lt;li&gt;1210 is the vertical rise of the lift. One trip down the ski run would show slightly more than that due to little hills in the middle of the run. None of those hills are higher than 100 feet so they don't make the watch think they are extra runs. &lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8140698" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben/archive/tags/BrainTeaser/default.aspx">BrainTeaser</category></item></channel></rss>