<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gary Ray</title>
    <description>The latest articles on DEV Community by Gary Ray (@geekcyclist).</description>
    <link>https://dev.to/geekcyclist</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F5521%2F1775420.png</url>
      <title>DEV Community: Gary Ray</title>
      <link>https://dev.to/geekcyclist</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/geekcyclist"/>
    <language>en</language>
    <item>
      <title>Modify IIS Site Log Location With PowerShell</title>
      <dc:creator>Gary Ray</dc:creator>
      <pubDate>Mon, 22 Feb 2021 07:00:00 +0000</pubDate>
      <link>https://dev.to/geekcyclist/modify-iis-site-log-location-with-powershell-f17</link>
      <guid>https://dev.to/geekcyclist/modify-iis-site-log-location-with-powershell-f17</guid>
      <description>&lt;p&gt;Over the years I have inherited multiple projects and shared web servers as I have moved teams. Often these servers have been setup or configured by several different people now long gone, and many times the defaults in IIS have never been changed. That can be a problem.&lt;/p&gt;

&lt;p&gt;Most recently I got a frantic call from a co-worker saying “all the sites on one of the production servers have quit working.” Scrambling, I logged in with Remote Desktop to check it out. Sure enough, this server (a VM actually) has a typical setup of a small OS drive partition (C:) and a much larger Data drive partition (E:). The C: partition was reporting only double digit megabytes of free space.&lt;/p&gt;

&lt;p&gt;Every Windows box I have dealt with in the past had an environment variable set for &lt;code&gt;%SystemDrive%&lt;/code&gt; and that has always pointed at the C: drive in my experience. So I checked the default location for IIS logs &lt;code&gt;%SystemDrive%\inetpub\logs\LogFiles&lt;/code&gt; and found that it contained over 120 GB of log data.&lt;/p&gt;

&lt;p&gt;Now, there are several issues that could be discussed, like monitoring so this problem was caught 30 or 40 GB earlier. Or like log storage, mining and truncation strategies. But, the immediate problem was that I have a Data drive partition that has nearly 1.5 TB of space, and I need to configure IIS to put the logs on that drive.&lt;/p&gt;

&lt;p&gt;I manually changed one of the sites log locations using IIS, and then realized that not only do I have 41 sites on this shared server, I also have a TEST/QA server with the same structure.&lt;/p&gt;

&lt;p&gt;Time for PowerShell.&lt;/p&gt;

&lt;p&gt;After a couple of tests, I arrived at the following script, saved as &lt;code&gt;switch-log-location.ps1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Import-Module&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;WebAdministration&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Set your desired root here...     &lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nv"&gt;$LogPath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"E:\IIS_Logs"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="kr"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$site&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ls&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;iis:\sites\&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nv"&gt;$PathExists&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Test-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$LogPath&lt;/span&gt;&lt;span class="nx"&gt;\&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-Not&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$PathExists&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;New-Item&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$LogPath&lt;/span&gt;&lt;span class="nx"&gt;\&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;directory&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Set-ItemProperty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;IIS:\Sites\&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;logFile.directory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LogPath&lt;/span&gt;&lt;span class="s2"&gt;\&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nv"&gt;$site&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Name&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In seconds, all of the sites will have their log location updated, and you can get started on cleaning up the cruft in the old location. Even better, the new location will have a folder name that matches the site name for every site on the shared server.&lt;/p&gt;

</description>
      <category>iis</category>
      <category>powershell</category>
    </item>
    <item>
      <title>What I Learned Teaching Python To Kids </title>
      <dc:creator>Gary Ray</dc:creator>
      <pubDate>Thu, 06 Jun 2019 00:54:59 +0000</pubDate>
      <link>https://dev.to/geekcyclist/what-i-learned-teaching-python-to-kids-1l7l</link>
      <guid>https://dev.to/geekcyclist/what-i-learned-teaching-python-to-kids-1l7l</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="http://agilecoder.net/2015/06/15/what-I-learned-teaching-python/"&gt;AgileCoder.net&lt;/a&gt; several years ago, but with the summer kids camp season upon us I thought I would repost it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Several years ago I volunteered at Kids Code Conference 2015 put on by &lt;a href="http://utahgeekevents.com/"&gt;Utah Geek Events&lt;/a&gt;. UGE puts on several free or very low cost coding and technology events every year including &lt;a href="https://twitter.com/hashtag/UtahCodeCamp"&gt;Utah Code Camp&lt;/a&gt; and &lt;a href="https://twitter.com/hashtag/bigmtndata"&gt;Big Mountain Data&lt;/a&gt;. I don't know how many times they have held a kids event, but this one was huge. &lt;a href="http://www.neumont.edu"&gt;Neumont University&lt;/a&gt; in downtown Salt Lake City hosted the event and provided an excellent venue.&lt;/p&gt;

&lt;p&gt;I had volunteered to help with the conference weeks earlier. It figured I would be able to wander around in the HTML/JavaScript or Python sessions and just help kids who were having trouble. I've spent some time teaching my own kids and one or two of their friends a little Visual Basic, or C# or Python, but I didn't think I was ready or able to teach a 3 hour workshop focused on developing a playable game. But about a little over a week before the conference one of the organizers who also happens to work for me at my main job said several instructors had dropped out and asked if I would be willing to teach one of the Python sessions.&lt;/p&gt;

&lt;p&gt;Let me clarify something. I don't use Python at all in my job. I barely use it at home. I don't develop games. I downloaded it and started playing with it because I had heard it was a reasonably simple language and had strong support for data analysis. That said. I was told that another experienced instructor was working on the outline and sample code, and that the workshop would be loosely based on the book &lt;a href="http://helloworldbook2.com"&gt;Hello, World! Computer Programming for Kids and Other Beginners&lt;/a&gt;. I had a copy, so I started working my way through it as fast as I could.&lt;/p&gt;

&lt;p&gt;Phil, the other instructor sent me the outline and the sample files and I dived in to getting ready to teach a room full of pre-teens and teenagers how to make a game with Python and Pygame. I originally thought I would have to teach both a morning and afternoon workshop, but as it worked out we were able to combine and work together. Phil led the morning workshop since I had never taught Python and I led the afternoon session.&lt;/p&gt;

&lt;p&gt;So, with that long introduction, here are the things I learned:&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation and Setup are Key
&lt;/h3&gt;

&lt;p&gt;In the morning session we ended up with about 30 kids. We had USB drives with sample files and installation files for &lt;a href="https://www.python.org/"&gt;Python&lt;/a&gt;, &lt;a href="http://www.pygame.org/"&gt;Pygame&lt;/a&gt; and &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; (rapidly becoming my go-to cross platform code editor). Kids and parents were sent an email with links and directions for how to setup the laptops the kids would be bringing from home. But even with that, I think only two or three of the kids entered the workshop with a computer ready to go. We ended up spending nearly the entire first hour installing and configuring kids computers. The kids who got it figured out or who we helped early quickly got bored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons:&lt;/strong&gt; Simplify. The &lt;a href="http://www.manning.com/sande2/"&gt;Hello, World book's web site&lt;/a&gt; has a very nice integrated installer for Mac, Windows and Linux. For the Utah Code Camp sessions on Python they recommended the Anaconda distribution that was easy to install (but didn't include Pygame which is why we didn't use it). In addition to trying an integrated installer, provide better pre-conference instructions, including how to verify that everything was installed and configured as expected. And while I am not a big fan of the IDLE editor included with Python, it's installed automatically and available on each platform, so it would have been simpler to use that and avoid the extra installation/configuration steps for Visual Studio Code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beware the Mac, Fear Linux
&lt;/h3&gt;

&lt;p&gt;Python comes pre-installed on most Apple MacBooks and is included by default with most Linux distributions. However, both the book and our curriculum were based on Python 2.7.x and most of the installations we saw were version 3.x, and none of them included Pygame. Neither Phil nor I are Mac or Linux experts, so helping the kids work through those installation problems was particularly difficult. It also presented a small challenge when we had kids copy files or access the terminal. We know the Windows shortcuts by heart, and have no problem right-clicking to view a context menu. It was hard to help the kids with those problems. We knew we would see at least a few MacBooks, but had no idea that at least a third of the kids in each class would be using one. Linux was far more rare. I think we only had one Linux user in each session.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons:&lt;/strong&gt; There are really two lessons here. First, the Mac users (or their parents) are likely to assume that if they can run Python they are ready, but won't worry about version or about Pygame. The fix here is likely the same as the first point; better installation and configuration instructions. The second is for me. I have access to an iMac and a MacBook and really should take advantage of them to prep for my next workshop like this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four to One is About Right
&lt;/h3&gt;

&lt;p&gt;It was really fortunate that we were able to combine classes, because for the morning session that allowed us to have Phil teach, and one other volunteer and I to roam to help. It wasn't enough. I would have been in trouble with both the installation problems and the actual teaching if it had just been me with 15-16 kids. We got lucky and in both sessions had at least one parent who could help out their own child and maybe one or two kids around them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons:&lt;/strong&gt; In the afternoon session we had a dad in the room with his own child and 3 friends, Phil and myself. Between the three of us it was still a bit of a scramble to make sure each kid had the help they wanted, or could be assisted if they were really stuck on something. I think a ratio of about 1 instructor/volunteer to 4 students is about right. If parents are around, they usually pick things up even if they aren't programmers, but can usually only help one or two kids, so there is a much higher need for volunteers for a Kids Coding Camp than an adult coding workshop.&lt;/p&gt;

&lt;h3&gt;
  
  
  You Will Only Cover Half of Your Outline.
&lt;/h3&gt;

&lt;p&gt;We had a great outline and Phil had put together a really organized set of source files with challenges. For instance, this was part of the source file for the topic "Lists". We would introduce the topic, show some examples and demos and then turn them loose for a few minutes on the challenges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This is a LIST (other languages call this an ARRAY)
&lt;/span&gt;&lt;span class="n"&gt;my_numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# ----  CHALLENGE 1 ----
# Display each number on its own line.
&lt;/span&gt;
&lt;span class="c1"&gt;# ----  CHALLENGE 2 ----
# Prefix each number with "Number: " and display it.
&lt;/span&gt;
&lt;span class="c1"&gt;# ----  CHALLENGE 3 ----
# Add the number 6 to the end of the list
&lt;/span&gt;
&lt;span class="c1"&gt;# ----  CHALLENGE 4 ----
# Display the third item in the list (the value 3).
&lt;/span&gt;
&lt;span class="c1"&gt;# ----  CHALLENGE 5 ----
# Replace the literal list with a call to the range() function.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We had about 16 topics from a Hello World intro, Basic Types, Loops, Functions... to an intro to Pygame, Sprites, Animation and finally a one player Pong-like game. Neither Phil in his morning session, nor me in the afternoon session made it anywhere near the Pygame content. Fortunately in the area of Functions and Classes we built a number guessing game, so we can honestly say that the kids did learn to build a game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons:&lt;/strong&gt; Kids are typically going to be starting completely cold, so you need to be prepared to cover very basic information. I think they had fun, learned a lot and left excited to learn programming. For a group of true novices I think the best you can hope for in three hours is to get through basic syntax, types, conditionals, loops, functions and classes. That gives you more than enough to build a text-based fortune-teller or a number guessing game. Talking with Phil after our classes I think the best format would be to do two beginner session in the morning, and then in the afternoon repeat a beginning session for those who had been in the Scratch or JavaScript sessions, and an intermediate session where you could focus on Pygame and build a more advanced game. This would have the added benefit of simplifying the setup for the beginning class because there would be less to install and configure.&lt;/p&gt;

&lt;h3&gt;
  
  
  If the kids aren't typing, moving or answering a question, you have lost them.
&lt;/h3&gt;

&lt;p&gt;It is incredibly easy to lose a kid's attention. In the morning session the setup hassle killed us, and even after we started teaching we had a hard time keeping everyone together and figuring out who might be struggling or bored. I found when I was teaching in the afternoon that I had to use all of the tricks I have ever learned in any teaching environment to keep the kids with me. Rather than use the defined and sometimes abstract examples I tried very hard to use examples that the kids would relate to. I asked them for help building the examples. I intentionally made simple coding mistakes and asked them to help me fix them. When doing challenges I would have them raise their hands when done, or have them get up and go stand by the wall, or turn their laptop around so I could see it. This allowed me to see who was struggling and have the ones who were picking things up easy to go try to help the ones who were struggling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons:&lt;/strong&gt; Kids are not adults. Then need different examples. They need very quick, easily digestible bits of information, and then they need to have very quick victories when doing the challenges. They need to feel like they are being successful and helpful. It's best, especially in a longer workshop, if you can figure out ways to have them stand up and move around frequently. For example, when teaching Lists/Arrays, make a physical "list" of students. Have two hold out their arms to make the brackets, and then append, delete, sort and find actual students between those two. Anything you do to keep them active and keep their attention will help you be successful.&lt;/p&gt;

&lt;p&gt;So there you have it. When getting ready and teaching kids coding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simplify the setup&lt;/li&gt;
&lt;li&gt;Be prepared for different environments if they bring their own computers&lt;/li&gt;
&lt;li&gt;Get close to 1-to-1 and no more than 4-1 if possible&lt;/li&gt;
&lt;li&gt;Manage expectations and cover the basics&lt;/li&gt;
&lt;li&gt;Have kid focused examples; make them small and engaging&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Good luck!
&lt;/h3&gt;

</description>
      <category>python</category>
      <category>teaching</category>
      <category>speaking</category>
    </item>
    <item>
      <title>Simplified Surrogate Keys in Oracle 12c and later</title>
      <dc:creator>Gary Ray</dc:creator>
      <pubDate>Wed, 05 Jun 2019 06:00:00 +0000</pubDate>
      <link>https://dev.to/geekcyclist/simplified-surrogate-keys-in-oracle-12c-and-later-21dl</link>
      <guid>https://dev.to/geekcyclist/simplified-surrogate-keys-in-oracle-12c-and-later-21dl</guid>
      <description>&lt;p&gt;In my job I work primarily in the Microsoft Stack, but I have to switch DBMS back ends frequently. Unfortunately I find it hard to keep up with changes sometimes and important updates pass me by. This is one that has been available for a few years now, but no one on my mostly SQL Server focused team knew about.&lt;/p&gt;

&lt;p&gt;Prior to Oracle 12c there was no built in support for AutoNumber or Identity columns as featured in several other RDMBS products. Instead the pattern was to create a Sequence/Trigger pair and rely on the pair to manage the values in the column.&lt;/p&gt;

&lt;p&gt;With the 12c release, Oracle now supports two options for similar behavior. Tables can be defined with a column as &lt;code&gt;NUMBER GENERATED ALWAYS AS IDENTITY&lt;/code&gt; to automatically generate a system linked sequence. Alternatively a table can be defined with a column with &lt;code&gt;DEFAULT ON NULL [sequence].NEXTVAL&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tables Using IDENTITY For Surrogate Keys
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creation
&lt;/h3&gt;

&lt;p&gt;The command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;default_test&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"ID"&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt; &lt;span class="k"&gt;GENERATED&lt;/span&gt; &lt;span class="n"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;IDENTITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nv"&gt;"DESCRIPTION"&lt;/span&gt; &lt;span class="n"&gt;VARCHAR2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="nb"&gt;BYTE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Results in a table created with this SQL generated by SQL Developer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"[owner]"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"DEFAULT_TEST"&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"ID"&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt; &lt;span class="k"&gt;GENERATED&lt;/span&gt; &lt;span class="n"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;IDENTITY&lt;/span&gt;
            &lt;span class="k"&gt;MINVALUE&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;MAXVALUE&lt;/span&gt; &lt;span class="mi"&gt;9999999999999999999999999999&lt;/span&gt;
            &lt;span class="k"&gt;INCREMENT&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;START&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;CACHE&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="n"&gt;NOORDER&lt;/span&gt; &lt;span class="n"&gt;NOCYCLE&lt;/span&gt;  
            &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nv"&gt;"DESCRIPTION"&lt;/span&gt; &lt;span class="n"&gt;VARCHAR2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="nb"&gt;BYTE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;
   &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SEGMENT&lt;/span&gt; &lt;span class="n"&gt;CREATION&lt;/span&gt; &lt;span class="k"&gt;DEFERRED&lt;/span&gt; 
   &lt;span class="n"&gt;PCTFREE&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="n"&gt;PCTUSED&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="n"&gt;INITRANS&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;MAXTRANS&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt; 
   &lt;span class="n"&gt;NOCOMPRESS&lt;/span&gt; &lt;span class="n"&gt;LOGGING&lt;/span&gt;
   &lt;span class="n"&gt;TABLESPACE&lt;/span&gt; &lt;span class="nv"&gt;"[owner]"&lt;/span&gt; &lt;span class="k"&gt;NO&lt;/span&gt; &lt;span class="n"&gt;INMEMORY&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Viewing the columns displays&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;COLUMN_NAME&lt;/th&gt;
&lt;th&gt;DATA_TYPE&lt;/th&gt;
&lt;th&gt;NULLABLE&lt;/th&gt;
&lt;th&gt;DATA_DEFAULT&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;NUMBER&lt;/td&gt;
&lt;td&gt;NO&lt;/td&gt;
&lt;td&gt;“[owner]”.”ISEQ$$_246189”.nextval&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Note how rather than having the traditional trigger/sequence pair, Oracle created a system generated sequence for you. Then it sets the default value for that row to the &lt;code&gt;nextval&lt;/code&gt; of the system generated sequence.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deletion
&lt;/h3&gt;

&lt;p&gt;If you want to delete the table, in this case the sequence is dropped at the same time.&lt;/p&gt;

&lt;p&gt;Executing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;default_test&lt;/span&gt; &lt;span class="n"&gt;PURGE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Removes both the table and the automatically generated sequence (and &lt;code&gt;PURGE&lt;/code&gt; removes them from the recycle area and makes them unavailable for flashback - careful).&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Details
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Viewing Identity Column details
&lt;/h4&gt;

&lt;p&gt;You can see the list of identity columns attached to tables by executing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;column_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;sequence_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;generation_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;identity_options&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;all_tab_identity_cols&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'[owner]'&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sample result&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;TABLE_NAME&lt;/th&gt;
&lt;th&gt;COLUMN_NAME&lt;/th&gt;
&lt;th&gt;SEQUENCE_NAME&lt;/th&gt;
&lt;th&gt;GENERATION&lt;/th&gt;
&lt;th&gt;IDENTITY_OPTIONS&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;APP_CONFIG&lt;/td&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;ISEQ$$_84588&lt;/td&gt;
&lt;td&gt;ALWAYS&lt;/td&gt;
&lt;td&gt;START WITH: 309, INCREMENT BY: 1, MAX…&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;APP_CONFIG_TYPES&lt;/td&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;ISEQ$$_84590&lt;/td&gt;
&lt;td&gt;ALWAYS&lt;/td&gt;
&lt;td&gt;START WITH: 3, INCREMENT BY: 1, MAX…&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  IDE Support
&lt;/h4&gt;

&lt;p&gt;SQLDeveloper Version 17.3.x does not appear to support creation of IDENTITY columns in the table creation dialog. Users have to edit the table creation script manually to create an Identity column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tables Using DEFAULT ON NULL For Surrogate Key
&lt;/h2&gt;

&lt;p&gt;Behavior similar to the IDENTITY usage above can be achieved using an explicit sequence and using the Oracle 12c enhanced &lt;code&gt;DEFAULT&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creation using DEFAULT ON NULL
&lt;/h3&gt;

&lt;p&gt;Executing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;SEQUENCE&lt;/span&gt; &lt;span class="n"&gt;seq_default2_test_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;default_test2&lt;/span&gt; 
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"ID"&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;SEQ_DEFAULT2_TEST_ID&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NEXTVAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nv"&gt;"DESCRIPTION"&lt;/span&gt; &lt;span class="n"&gt;VARCHAR2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="nb"&gt;BYTE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Will create a sequence, a table, and will cause the ID field to behave like an AutoNumber or Identity field.&lt;/p&gt;

&lt;p&gt;The SQLDeveloper IDE generated SQL for this table is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"[owner]"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"DEFAULT_TEST2"&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;"ID"&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt; 
            &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="nv"&gt;"[owner]"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"SEQ_DEFAULT2_TEST_ID"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;"NEXTVAL"&lt;/span&gt;
            &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;"DESCRIPTION"&lt;/span&gt; &lt;span class="n"&gt;VARCHAR2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="nb"&gt;BYTE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="n"&gt;ENABLE&lt;/span&gt;
   &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SEGMENT&lt;/span&gt; &lt;span class="n"&gt;CREATION&lt;/span&gt; &lt;span class="k"&gt;DEFERRED&lt;/span&gt;
   &lt;span class="n"&gt;PCTFREE&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="n"&gt;PCTUSED&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="n"&gt;INITRANS&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;MAXTRANS&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;
   &lt;span class="n"&gt;NOCOMPRESS&lt;/span&gt; &lt;span class="n"&gt;LOGGING&lt;/span&gt;
   &lt;span class="n"&gt;TABLESPACE&lt;/span&gt; &lt;span class="nv"&gt;"[owner]"&lt;/span&gt; &lt;span class="k"&gt;NO&lt;/span&gt; &lt;span class="n"&gt;INMEMORY&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that in neither case is an explicit primary key constraint generated by default.&lt;/p&gt;

&lt;h4&gt;
  
  
  DEFAULT vs. DEFAULT ON NULL
&lt;/h4&gt;

&lt;p&gt;If the sequence is linked using &lt;code&gt;DEFAULT&lt;/code&gt; alone rather than &lt;code&gt;DEFAULT ON NULL&lt;/code&gt; there are two significant differences:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The field is not automatically designated as &lt;code&gt;NOT NULL&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;An insert that explicitly passes &lt;code&gt;NULL&lt;/code&gt; to the ID column (as often occurs with &lt;code&gt;INSERT&lt;/code&gt; statements generated by ORMs) will succeed and will likely cause referential integrity errors or downstream data integrity problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Viewing the table columns in SQLDeveloper displays:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;COLUMN_NAME&lt;/th&gt;
&lt;th&gt;DATA_TYPE&lt;/th&gt;
&lt;th&gt;NULLABLE&lt;/th&gt;
&lt;th&gt;DATA_DEFAULT&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;NUMBER&lt;/td&gt;
&lt;td&gt;NO&lt;/td&gt;
&lt;td&gt;“[owner]”.”SEQ_DEFAULT2_TEST_ID”.”NEXTVAL”&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Deleting Default Sequences
&lt;/h3&gt;

&lt;p&gt;In this case the deletion of the table will not delete the sequence automatically. It is also possible to drop the sequence without dropping the table, which due to the dependency on the sequence will cause a runtime error if the insert does not include a value for the ID column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SQL Error: ORA-02289: sequence does not exist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To drop both objects execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="n"&gt;SEQUENCE&lt;/span&gt; &lt;span class="n"&gt;seq_default2_test_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;default_test2&lt;/span&gt; &lt;span class="n"&gt;PURGE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Switch?
&lt;/h2&gt;

&lt;p&gt;Documentation in the resources listed below states&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Not surprisingly, [a trigger/sequence-based table] performs much worse than the others. The direct use of a sequence [using &lt;code&gt;DEFAULT&lt;/code&gt;] and the 12c identity column give comparable results, which are typically an order of magnitude faster than using a trigger to populate the ID column. &lt;a href="https://oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1#performance"&gt;link&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you are using small tables in a situation where you do very infrequent inserts, then sticking with a trigger/sequence pair is fine and will be easily understood by developers who have worked with older Oracle versions.&lt;/p&gt;

&lt;p&gt;If however you have any concerns about scale and load, I would recommend switching to one of the two newer techniques.&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Choose Which Technique To Use
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;"ID" NUMBER GENERATED ALWAYS AS IDENTITY&lt;/code&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have no need to access the sequence directly (see related bullet for &lt;code&gt;DEFAULT&lt;/code&gt; below)&lt;/li&gt;
&lt;li&gt;You want to ignore the creation of the sequence and let the DB handle that automatically.&lt;/li&gt;
&lt;li&gt;You don’t mind your schema being cluttered with sequences named like ISEQ$$_219376&lt;/li&gt;
&lt;li&gt;You want the DB to automatically handle removing the sequence if you remove the table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use &lt;code&gt;"ID" NUMBER DEFAULT ON NULL [sequence_name].nextval&lt;/code&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need access to the returned sequence value as part of a multi-step process or transaction in an API, business layer or ORM and are going to insert it manually. In this case you use a sequence that meets your organizations naming convention.&lt;/li&gt;
&lt;li&gt;You have a strict naming convention for sequences and want to avoid the auto-generated sequence name.&lt;/li&gt;
&lt;li&gt;You need to ensure that sequence names are identical between Dev, Test and Production schemas.&lt;/li&gt;
&lt;li&gt;You are ok with managing the deletion or modification of both objects if one of them is dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1"&gt;Identity Columns in Oracle Database 12c Release 1 (12.1)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://oracle-base.com/articles/12c/default-values-for-table-columns-enhancements-12cr1"&gt;DEFAULT Values for Table Columns : Enhancements in Oracle Database 12c Release 1 (12.1)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.oracle.com/oraclemagazine/improved-defaults-in-oracle-database-12c"&gt;Improved Defaults in Oracle Database 12c&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>oracle</category>
    </item>
    <item>
      <title>Testing Odd/Even Using Bitwise AND</title>
      <dc:creator>Gary Ray</dc:creator>
      <pubDate>Tue, 31 Jul 2012 06:00:00 +0000</pubDate>
      <link>https://dev.to/geekcyclist/testing-odd-even-using-bitwise-and-ak9</link>
      <guid>https://dev.to/geekcyclist/testing-odd-even-using-bitwise-and-ak9</guid>
      <description>&lt;p&gt;I came to the programming world by way of an Apple II, TI 99/4A, BASIC, VB/VBA and on to C#. But my academic experience is in the world of economics, not computer science. So occasionally I come across things that are really basic (I assume) which I have clearly forgotten somewhere between Apple Basic, High School Electronics, and the ensuing decades of other trivia crammed into my brain.&lt;/p&gt;

&lt;p&gt;The Bitwise &lt;code&gt;AND&lt;/code&gt; is the operator that left me feeling stupid today.&lt;/p&gt;

&lt;p&gt;Many times in my career I’ve come across code to test whether a number is odd or even. Usually I have done or seen modulo division by 2 and a comparison of the remainder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;IsOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Today I came across:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;IsOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I had no idea why that worked. I looked at the ms help:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Binary &amp;amp; operators are predefined for the integral types and bool. For integral types, &amp;amp; computes the bitwise AND of its operands. For bool operands, &amp;amp; computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not particularly enlightening without doing some math, so I dug a little deeper. Reviewing binary counting I made a table on a 3x5 card, assuming a 4-bit number for simplicity:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decimal&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;5&lt;/th&gt;
&lt;th&gt;6&lt;/th&gt;
&lt;th&gt;7&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1000&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Then I started thinking about the operation, but I initially processed it in my head as a binary addition not as the proper bitwise operation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;3 + 1&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;=&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;But that was clearly not right, since the result is 4, and for any positive integer greater than 0 the test would fail if addition was the underlying action. I looked up a couple more definitions for bitwise AND and finally understood this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A bitwise AND operation &lt;code&gt;(a &amp;amp; b)&lt;/code&gt; returns a one in each bit position for which the corresponding bits of both a and b are positive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The result is a logical test for each bit:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;3 &amp;amp; 1&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;=&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Since the binary representation of decimal 1 is 0001, (front padded with as many zeros as necessary to match the bit size of the number), the only way the operation ([any integer] &amp;amp; 1) will return 1 is if the rightmost bit is one – all odd numbers.&lt;/p&gt;

&lt;p&gt;The following table describes the bitwise operators in C# I find myself using now.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Usage&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bitwise AND&lt;/td&gt;
&lt;td&gt;&lt;code&gt;a &amp;amp; b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a one in each bit position for which the corresponding bits of both a and b are ones.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bitwise OR&lt;/td&gt;
&lt;td&gt;&lt;code&gt;a | b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a one in each bit position for which the corresponding bits of a OR b or both a AND b are ones.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bitwise XOR&lt;/td&gt;
&lt;td&gt;&lt;code&gt;a ^ b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a one in each bit position for which the corresponding bits of either a OR b but not both a AND b are ones.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bitwise NOT&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Inverts each bit of a.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For more on how Binary works, see &lt;a href="http://www.swansontec.com/binary.html"&gt;swansontec.com/binary.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As an aside - I had never really thought about whether zero is even or odd. Turns out zero is even, and here’s why &lt;a href="http://en.wikipedia.org/wiki/Parity_of_zero"&gt;wikipedia.org/wiki/Parity_of_zero&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
