PowerBASIC Gazette

Number 114 www.powerbasic.com


From: Bob Zale, President
          PowerBASIC, Inc.

To: PowerCODER

PowerBASIC Gazette #114

Subject: Programming with BIT FIELD variables

Welcome to the latest edition of the PowerBASIC Gazette!

We'd like to go back and review BIT FIELD variables. Certainly an underused but important feature of the latest PowerBASIC Compilers.

Of course, if you still haven't upgraded to PowerBASIC 10 for Windows or PowerBASIC Console Compiler 6, now's the time to do it! Choose delivery by download, and you could be "up and running" 30 minutes from now!

But more about that later...

Bit Field Variables

PowerBASIC offers a wide range of variable types. Signed. Unsigned. 8-bit, 16-bit, even 32-bit and 64-bit. But, sometimes, that just isn't enough. For example, there are many times when you only need to store a value for true/false or on/off... it takes just one bit to represent the value 0 or 1. Why waste a whole variable for that? If you need to represent the day of the week, you might use the values 1 to 7. Those particular values can be represented in just 3 bits. Why waste a regular variable for that, too?

Well, it's now no longer necessary. Bit Field variables to the rescue! Use them to design your own custom variables, anywhere from 1-bit wide to a full 31-bits. You can pack these variables in a User-Defined Type or Union, one after another, to gain the very best in memory efficiency.

You can use them for compatibility, too. They're common in C and C++. The Windows API uses them extensively. Even if you don't need them now, it's good to understand them... and good to know they'll be right at your fingertips the day you find you really need this power.

Bit Field variables come in two flavors, signed and unsigned. Just as with regular integer-class variables, the signed variety can be used to store both positive and negative values, while unsigned are restricted to just positive values. Internally, the signed versions use one bit for the sign, and the remaining bits for the numeric value, while the unsigned use all the bits for the value.

Signed Bit Field variables are called SBIT variables, while the unsigned variety are simply called BIT variables. They are only valid as a member of a User-Defined TYPE or UNION, because they are tightly packed, one after another, in order to optimize for the very minimum amount of memory usage. This can be critically important when you create a large array of these data types.

As I said earlier, BIT variables are custom variables -- you decide the size, and you decide the format. For example, "BIT * 1" defines a 1-bit unsigned variable which may take the value 0 or 1. The following table shows the range of values which may be stored in Bit Field variables of each possible size. The first two columns list the range for unsigned BIT variables, while the next two columns denote the range for the same size signed SBIT variables:

1 bit -->>0 to 1-1 to 0
2 bits -->>0 to 3-2 to +1
3 bits -->>0 to 7-4 to +3
4 bits -->>0 to 15-8 to +7
5 bits -->>0 to 31-16 to +15
6 bits -->>0 to 63-32 to +31
7 bits -->>0 to 127-64 to +63
8 bits -->>0 to 255-128 to +127
9 bits -->>0 to 511-256 to +255
10 bits -->>0 to 1023-512 to +511
11 bits -->>0 to 2047-1024 to +1023
12 bits -->>0 to 4095-2048 to +2047
13 bits -->>0 to 8191-4096 to +4095
14 bits -->>0 to 16383-8192 to +8191
15 bits -->>0 to 32767-16384 to +16383
16 bits -->>0 to 65535-32768 to +32767
17 bits -->>0 to 131071-65536 to +65535
18 bits -->>0 to 262143-131072 to +131071
19 bits -->>0 to 524287-262144 to +262143
20 bits -->>0 to 1048575-524288 to +524287
21 bits -->>0 to 2097151-1048576 to +1048575
22 bits -->>0 to 4194304-2097152 to +2097151
23 bits -->>0 to 8388608-4194304 to +4194303
24 bits -->>0 to 16777215-8388608 to +8388607
25 bits -->>0 to 33554431-16777216 to +16777215
26 bits -->>0 to 67108863-33554432 to +33554431
27 bits -->>0 to 134217727-67108864 to +67108863
28 bits -->>0 to 268435455-134217728 to +134217727
29 bits -->>0 to 536870911-268435456 to +268435455
30 bits -->>0 to 1073741823-536870912 to +536870911
31 bits -->>0 to 2147483647-1073741824 to +1073741823

Of course, you should use care. Just like so-called "regular" variables, if you try to assign a value to a Bit Field variable which is outside the legal range, you get undefined results.

So, how are BIT variables defined? As mentioned earlier, they must be a part of a User-Defined TYPE. As the programmer, you get to define the size of the entire field, as well as the BIT variables which make up that field. Each field may be 1, 2, or 4 bytes in size, and there may be any number of these fields in a TYPE. You'll use the word BYTE, WORD, or DWORD to specify the field size:

TYPE ByteParts
  Nybble1 as BIT * 4 in BYTE
  Nybble2 as BIT * 4

In the above example, the phrase "in BYTE" specifies that the field is one byte in total size, and accessible through either of the two 4-bit nybbles. Each of the 4-bit nybbles can store a value in the range of 0 to 15. An alternative could be to use signed variables in a similar structure:

TYPE ByteParts
  Nybble1 as SBIT * 4 in BYTE
  Nybble2 as SBIT * 4

The only difference here is that each of the 4-bit nybbles can store a signed value, in the range of -8 to plus 7. It's really just that easy! What if you need a larger field? No problem...

TYPE DWordParts
  Nybble1 as BIT * 4 in DWORD
  Nybble2 as BIT * 4
  Nybble3 as BIT * 4
  Nybble4 as BIT * 4
  Nybble5 as BIT * 4
  Nybble6 as BIT * 4
  Nybble7 as BIT * 4
  Nybble8 as BIT * 4

Now, the phrase "in DWORD" tells us that the field is a total of four bytes in total size, and it's accessed through any one of the eight member nybbles.

Remember, a User-Defined TYPE may contain one bit field, or many bit fields. You're the programmer, so you're in control! Let's say you need to separate a LONG INTEGER variable into its component parts, and you also need to maintain a number of true/false values as well. Here's a simple way to do just that:

Type abcd
  Valu as BIT * 31 in DWORD
  Sign as SBIT * 1
  Flag as BIT * 1 in BYTE
  Text as BIT * 1
  Slot as BIT * 1
  Coin as BIT * 1
  Dart as BIT * 1
End Type

In the above example, the entire TYPE is 5 bytes in size, consisting of seven member variables. You may have noticed that only 5 bits were used in the second bit field. It's perfectly acceptable to leave some bits unused if they're not needed. But be sure you never try to exceed the size... You simply can't fit 9 bits in 1 byte!

How do you access BIT and SBIT variables? Just like any other member of a User-Defined TYPE:

DIM Structure as abcd
Structure.Coin = 1
Structure.Slot = 0
IF ISTRUE Structure.Coin THEN
  MSGBOX "We have a valid coin."

I hope it's now clear that bit field variables can greatly compress your data size. They can make more code self-documenting. They'll give you compatibility with certain "C" code, and assist with the Windows API. Regardless of the application, it's a powerful tool for the programmer... and found only in your favorite brand of BASIC!

You can find a complete copy of the PowerBASIC Documentation on the PowerBASIC Web Site at:


PowerBASIC Forums

Be sure to visit us soon on the web! Lots of options to help you find the exact programming information you need! The PowerBASIC Forums now sport over 400,000 contributions from good programmers just like you. They ask questions, they get answers, so can you. Just click...


We'd love to hear your questions... your answers... maybe even see a little of your latest code. So would others, and they'll respond!

Lots more to follow on the web site... and the next Gazette!

THE POWER Changes Everything

Just in case you're one of the few... who hasn't yet upgraded to the latest compilers -- Stay with me for a minute?

Now is the time to move. Full product purchases are just $199 and $169. Pretty refreshing compared to some of the $2,000-$5,000 prices from the competition! If you qualify for an upgrade, you can still take advantage of special pricing. Upgrade PB/WIN 9 to PowerBASIC 10.0 for just $99 -- upgrade PB/CC 5 to PB/CC 6.0 for just $89! There is no reason to wait... today is the day to upgrade.

Of course, PB/CC is our Console Compiler -- creates programs with a text mode user interface. Easy to use. Easy to port from DOS. It's the perfect solution for CGI Internet applications. Any time you want pure performance and nothing more! PowerBASIC 10 for Windows gives you the "look and feel" of a graphical user interface (GUI), the essence of Windows. Frankly, they've both taken a big leap forward.

Transparent Unicode

You know, a good compiler offers you ANSI strings. This has been the standard for many years. A better compiler lets you choose between ANSI and UNICODE in each program. But only one of them per program. If you want Unicode, you can't keep binary bytes in a string. It simply won't work. If you want ANSI, you can't have Unicode without exhaustive conversions. Not so good.

A great compiler, like PowerBASIC, supports all of them in the same program. And it's totally transparent. With PowerBASIC 10.0 for Windows (or PowerBASIC Console Compiler 6.0), you can have it all. One variable with ANSI. Another with UNICODE. Mix and match any way you choose with PowerStrings. All the messy details, and even the needed translations, are handled automatically by the compiler.

You can finally display a Euro symbol. Unicode text in a GUI or a Console. Read and write a Unicode file. Even print Unicode text on a Windows printer!

Static Link Libraries

Compile all your general purpose code into static units. Then, when it's needed, just $LINK it into your EXE or DLL and you're on your way. It's pre-compiled, so it's perfect for team programming. What will it do for third-party tools? Plenty. PerfectSync has already released SQL Tools 3. It links right into your EXE, so there's no need to drag a DLL around. Just $LINK and go. Lots more tools are sure to follow. And, if that's not enough, you'll get a librarian, too. Combine any number of units to a single library, and just use one single $LINK. PowerBASIC just discards those which aren't necessary. SLL's can contain Subs, Functions, and Object Classes. How easy can it get?

Automatic Dead Code Removal

Fight the war on BloatWare. Now, you can include big libraries of useful code -- PowerBASIC cleans it up for you. Any Sub/Function not used is automatically ignored when compiled. In fact, even Classes are pared down by similar Method and Property removal.

Order Now?     GOTO https://shop.powerbasic.com/

There's more. Lots, lots more. A completely new IDE based on Tabs, with syntax display as you type! Transparent Unicode. Print Preview. Thread Objects. A built in Resource Compiler. A StringBuilder Class. PowerArray class encapsulates SafeArray structures. A DEC$ function formats decimal numbers. Graphic Windows with scroll bars and scroll keys, user drag to resize, clip areas, and wrap by whole word. ENUM blocks, PREFIX blocks, ASMDATA blocks. THREADSAFE option for functions and methods. TEXT windows, Text Split, and the new FASTPROC procedures. There's more, but it's easy to see this is a very important upgrade.

SQL Tools version 3

You can use the power of SQL to access relational databases from Microsoft Access, SQL Server, Oracle, FoxPro, dBase, Btrieve, and 50 other popular formats. Open a database with a single line of code, and use standard SQL statements to build powerful, sophisticated, multi-user database programs! This is a total database solution.

This product has earned accolades from virtually all of its users. Version 3 is certainly no exception. With both a DLL and an SLL, you'll have the option to just $LINK units and libraries right into your EXE. No more DLL's to drag around, unless you choose it. Even better, PowerBASIC will only link the code that's necessary. Version 3 is faster and leaner, even with all the new features.

Many functions have been simplified. You can retrieve all rows in a single operation. You'll also get improved trace files, Quad integers, Unicode, plus enhanced Memo and BLOB field support. Then there's improved Microsoft Access database support, and much more! Documentation is provided in CHM, HLP, and PDF formats.

You can order right now...

PB/WIN 10.0 is priced at $199, while PB Console Compiler is $169. Upgrades from versions 9 and 5 are just $99 and $89 respectively. SQL Tools Pro is $199.95, while SQL Tools Std is $99.95. Upgrades from version 2 are $99.95 and $69.95 respectively.

Need more info?

We have a page created just for you. It's "Why should I upgrade?"

GOTO http://www.powerbasic.com/products/whyupcc.asp
GOTO http://www.powerbasic.com/products/whyupwin10.asp

Need even more info?

The complete documentation for both PB/CC 6 and PB/WIN 10 can be found right on the PowerBASIC Web Site! To see the 100% complete documentation:

          GOTO www.powerbasic.com -- Then [click] HELP DESK

You'll find everything you need to know to make an informed decision. You can order right now by replying to this email. You can call us today at (888)659-8000 or (941)473-7300, or fax us at (941)681-3100.

You can visit https://shop.powerbasic.com/ to place an e/order on our secure web site, or even mail an order to our offices. But no matter what method you choose, do it today and do it with confidence. Every product PowerBASIC ships for physical delivery is offered with a money-back guarantee for a full 30 days from the transaction date.


Bob Zale, President
PowerBASIC Inc.

PowerBASIC Price List

PB/CC Console Compiler 6.0 - Full Product $169.00
PB/CC Console Compiler 6.0 - Upgrade from ver 5 $89.00
PB/CC Console Compiler 6.0 - Upgrade from prior versions $119.00
   Add Printed Documentation $49.00
PowerBASIC for Windows 10.0 (GUI) - Full Product $199.00
PowerBASIC for Windows 10.0 - Upgrade from ver 9 $99.00
PowerBASIC for Windows 10.0 - Upgrade from prior versions    $129.00
   Add Printed Documentation $49.00
Classic Console Compiler 5.0 - Full Product $89.00
Classic Console Compiler 5.0 - Upgrade from ver 4 $49.00
Classic PB for Windows 9.0 (GUI) - Full Product $99.00
Classic PB for Windows 9.0 - Upgrade from ver 8 $49.00
PowerBASIC for DOS 3.5 - Full Product $99.00
PowerBASIC for DOS 3.5 - Upgrade from prior versions $49.00
   Add Printed Documentation (2 book set) $29.00
PowerTree BTree Manager for DOS and Windows $99.00
PowerBASIC Forms Visual Designer ver 2.0 $99.00
SQL Tools Standard Version 3.0: $ 99.95
   Upgrade from ver 2 (Std) $69.95
SQL Tools Professional Version 3.0: $199.95
   Upgrade from ver 2 (Pro) $99.95
   Upgrade from ver 2 (Std) $129.95
Graphics Tools Standard ver 2 for PB/CC & PB/WIN $69.95
Graphics Tools Professional ver 2 for PB/CC & PB/WIN $139.95
Graphics Tools Standard ver 2 Upgrade from ver 1 $44.95
Graphics Tools Professional ver 2 Upgrade from ver 1 $114.95
Graphics Tools Professional ver 2 Upgrade from ver 2 Std $79.95
Console Tools Standard: $49.95
Console Tools Professional: $99.95


Any SoftwareSoftware
& 1 book
Addl Book
UPS Ground/Mail US$10$10$ 8
Express 2-day US$18$18$14
Express 1-day US$28$35$30
Air Mail Canada/Mex    $10$18$18
Express Canada/Mex$30$40$34
Air Mail Intl$14$28$28
Express Intl$36$46$40

Order online at shop.powerbasic.com/ or just send an email with all pertinent information to: sales@powerbasic.com

We'll take it from there!

Most PowerBASIC products (those without printed books) can now be delivered by electronic mail. No wait for a package to arrive... No high shipping costs... For just $6 per order, no matter how many products, we'll deliver directly to your computer. If you're outside the U.S., savings might be greater. You won't pay taxes or duties to a freight company or postal service, because they aren't involved in the delivery. Check your tax code to be sure, but some countries charge no tax at all on transactions of this type. It could just be your lucky day!

Send your subscription request to email@powerbasic.com and please include your name and all email addresses you'd like to add, as well as your Zip or Postal Code. If you know someone else who would enjoy this newsletter please forward a copy to them so they can subscribe.

All contents Copyright (c) 2012 PowerBASIC Inc All Rights Reserved. PowerBASIC, PB/CC, PowerBASIC Forms, and PowerTREE are trademarks of PowerBASIC Inc. Other names are trademarks or registered trademarks of their respective owners.

PowerBASIC Gazette - Electronic Edition
Volume 1 - Issue 114
PowerBASIC, Inc. (888) 659-8000 Sales
2061 Englewood Road (941) 473-7300 Voice
Englewood, FL 34223     (941) 681-3100 Fax

Visit us on the Net at www.powerbasic.com    Email Sales at sales@powerbasic.com

This newsletter is only sent to email addresses in our subscription list. If you have received this newsletter by mistake or no longer wish to receive it, please forward this newsletter, with "unsubscribe" in the message to support@powerbasic.com with your name and zip/postal code.