Tips and Techniques: Say Goodbye to Binary Fields

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

I'm sick of it. If I see one more line of new code that uses a B data type, I'm going to blow a vein in my neck.

The old B (binary) data type was retired back in OS/400 V3. In its place, IBM introduced and recommended the new I (integer) data type. The I data type is compatible with the old B data type as well as with the C, C++, and Java languages and the new TYPE(*BIN) support in V5R3 CL.

The I data type is much more efficient than B was. It performs faster because no conversion to/from packed occurs. B data types are mapped into packed under the covers in the compiler to allow decimal positions. I have never heard of one single instance where a B field used decimal positions (and if you have used decimal positions in a B field, I recommend that you keep it to yourself).

What's so great about the I data type? One thing is the speed; integer fields are so fast that I now use them as my default data type for numeric work fields.

Of course, if you need decimal positions, you need to continue to use packed fields for those applications. For all others, use the I data type.

To specify an I data type, you need to specify the number of digits that can be stored in the value (not an intuitive task) and the I data type itself. You also need to specify the number of decimal positions, which is always 0.

For example, to declare a 4-byte integer field, the following D specification can be used:


D Int4 S 10I 0

Field INT4 is declared. You pronounce the length and type as a "ten eye zero" value.

I believe this is why so many people continue to use B (binary) fields. It may be too confusing to use 10 for the field's length, when it is actually 4 bytes. Specifying a 0 for decimal positions seems like overkill to me, but it's not a showstopper.

The following illustrates the size of each available I data type, along with the declaration option for that field size.

    

This is a 3I0 value, also known as Int1.

    


    

And here's a 5I0 value, also known as Int2.

    

    
    

    


And a 10I0 value, also known as Int4.

    

    
    
    




    



    
    
    

And finally, a 20I0 value, also known as Int8.

The API documentation often refers to Bin(4) and Bin(2) values. When a Bin(4) value is specified, the 10I0 value should be used; if Bin(2) is requested, then 5I0 is appropriate. In fact, I data types are more-correct data types than the old B data types when using OS/400 APIs.

Short, Long, and Long Long?

You'll note that in other languages, the way you declare the so-called "integer" data type is not always intuitive. Below is a table that illustrates the cross-reference for I data types in RPG IV and other languages.


Int 1
Int 2
Int 4
Int 8
RPGIV
3I 0
5I 0
10I 0
20I 0
C
char
short
int or long
long long
C++
char
short
int or long
long long
SQL
N/A
SMALLINT
INT
BIGINT

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: