Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


July 2009

Understanding Bitmap Values


RSS
Subscribe to Windows IT Pro | See More Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Emulating the Dir Command in PowerShell

Executive Summary:
Here's a quick review of how bitmap values work and how you can use them with Windows Powershell for passing information between functions. You can use bitwise operators and masks to manipulate bitmap values.


The D.ps1 script described in "Emulating the Dir Command in PowerShell" relies on bitmap values for passing information between functions. A bitmap value is a concise way of storing a list of Boolean (i.e., true/false) values. You can think of a bitmap value as an array of bits rather than a discrete number. Each bit in the number is set to either 1 (enabled) or 0 (disabled). You can combine a bitmap value with a mask by using bitwise operators (in Windows PowerShell, these are -band, -bor, -bnot, and -bxor) so that you can read, set, clear, and toggle bits in the bitmap value. A mask is a number that represents the bit or bits you're interested in. Mask values are always powers of two.

File attributes are a good example of how the OS uses bitmap values. The following PowerShell command lists the .NET System.IO.FileAttributes enum and the associated mask value for each attribute:

[Enum]::GetValues([System.IO.FileAttributes]) |
select-object @{"Name" = "Name"; "Expression" = {$_}},
@{"Name" = "Value"; "Expression" = {[Int] $_}} |
format-table -auto

Figure A shows this command and its output.

From Figure A, we can see that a mask of 1 represents the read-only attribute, 2 (10 in binary) represents the hidden attribute, 4 (100 in binary) represents the system attribute, 16 (1000 in binary) represents the directory attribute, and so forth. You can combine multiple values with the -bor operator to create a multibit mask. For example, the mask 2 -bor 32 combines the hidden (2) and archive (32) attributes.

You can get a file's attributes by reading its Attributes property, which is really the .NET type System.IO.FileAttributes. If you cast the property as an integer by using [Int], you get the Attributes property's bitmap value. Consider the following example:

PS > $attrs = (get-item c:\test.txt).Attributes
PS > $attrs
ReadOnly, Archive
PS > [Int] $attrs
33

The first command retrieves c:\test.txt's attributes; the second command displays the $attrs variable, which PowerShell outputs as a list of attributes; and the third command displays the $attrs variable as an integer (i.e., its corresponding bitmap value).

PowerShell automatically converts the .NET values to integers in comparisons, which lets you use code such as the following:

PS > [System.IO.FileAttributes]::ReadOnly -eq 1
True
PS > [System.IO.FileAttributes]::Hidden -eq 2
True

The following formulas describe how to set and change the bits in a bitmap value:

  • Check to see if at least one bit is set: (bitmap -band mask) -ne 0
  • Check to see if all of the bits are set: (bitmap -band mask) -eq bitmap
  • Check to see if none of the bits are set: (bitmap -band mask) -eq 0
  • Set one or more bits: bitmap = bitmap -bor mask
  • Clear one or more bits: bitmap = bitmap -band (-bnot mask)
  • Toggle one or more bits: bitmap = bitmap -bxor mask

Using the $attrs variable from the previous example, we can use code such as the following:

($attrs -band 1) -ne 0 # True because the read-only bit is set
($attrs -band 2) -ne 0 # False because the hidden bit is not set
($attrs -band 32) -ne 0 # True because the archive attribute is set
($attrs -band (1 -bor 32)) -eq $attrs # True because both the
read-only and archive bits are set

The D.ps1 script described in "Emulating the Dir Command in PowerShell" uses bitmap values and masks in its get-attributeflags and main functions, which you can see in the listing with that article.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events WinConnections and Microsoft® Exchange Connections

PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement