Java IoT Authors: Elizabeth White, Liz McMillan, Yeshim Deniz, Pat Romanski, Roger Strukhoff

Related Topics: Java IoT

Java IoT: Article

Bit Fiddling in Java with No 'Bit' Type

The algorithmic-intensive no-man's land made habitable

When looking for a language to modernize legacy applications, Java is a strong and viable contender. It offers portability, maintainability, extensibility, and cost effectiveness. However, for some heavy algorithmic time-critical scientific applications, Java may not be an engineer's first choice for modernization. Many algorithmic-intensive scientific applications require bit-wise manipulations for which Java has no real clean solution. This is all true, but with a little creativity, Java can handle bit-wise manipulations.

There is no Java bit class. Java handles bit-wise comparisons and shifting, but currently there's no Java class that handles a native bit type. The closest native type in size is a Boolean. Varied sized data fields require a container that can handle various size data fields and still treat the data fields as objects.

Java does have a BitSet class that can handle bits. The Java BitSet class implements a vector of bits that expands as needed. Each item in the vector has a Boolean value. A BitSet is really a vector of bits, and can be used to efficiently store a lot of on-off information. The minimum size of the BitSet is 64-bits; the same length as a long. If you're storing a value that's a smaller size such as 3-bits the BitSet is excessively large and inefficient. If efficient access is required accessing bits from a BitSet would be slower than accessing an array of some native type.

Based on these two limiting factors creating a storage unit to handle the storage of the bits would be a better approach. The most ideal situation would be to have a bit native type. Since there's no Bit class available in Java, creating a class to handle bits of variable size would be the best solution.

A creative Java developer can create his own Java bit class simply by extending the Boolean class:

public class MyBitType extends Boolean {}

The newly defined MyBitType can then be used to hold either a single bit or multiple bits by creating an array of MyBitType. The array of MyBitType could then be various lengths depending on the user's need.

The problem with the MyBitType class is that inside a Java Virtual Machine (JVM) there are no instructions specifically for Booleans. A Boolean doesn't have a defined size and its JVM implementation is free to store the Boolean in any fashion it chooses. Often integer operations are used on bits so it's very possible that a single Boolean is stored as an integer and an array of Booleans could be stored as an array of bytes which is very inefficient in terms of both storage and access. This, of course, would need to be verified by looking at the source code for the JVM being used.

An even more creative software engineer might choose to create a BitContainerType class. This BitContainerType class would define the size of the container to store the data values. By creating a defined size, the user should gain efficiency.

In addition to containing a data field value, this BitContainerType class can contain the bit starting position and could be used to assemble messages based on the starting position and the length of the data field. This makes the data an object that can be manipulated in Java (see Listing 1).

The BitContainerType for a message can be defined as shown in Listing 2.

Besides defining the values, the BitContainerType class has get and set methods for accessing the data field values. The data fields are now BitContainerType objects that can be reused by other parts of the application. The defined sized objects can now be stored in the data base, interface with the model code, or be reused by any of the application code.

More importantly, the varied sized data fields are objects with a length and value that can be put together or "fiddled with" to make a message. The objects must be shifted and masked to assemble them as a contiguous stream of bits. This stream can be efficiently parsed by the receiving interface to access fields defined anywhere in the message. Note from the sample code above that the data values are all different lengths and that a different number of bits would be very tedious and inefficient to process without a bit-like structure like the BitContainerType.

The individual messages will be created as a byte array that will consist of 128 bytes. If the values are always going to stay the same, no further processing is required.

If the data is dynamic and constantly changing, then a methodology for changing the values contained in the BitContainerType data objects must be defined. The ultimate goal is to communicate with the interfaces via the messages. The messages must be built before being sent. The messages are built by combining the varied size BitContainerType objects in the specified order to be sent.

The BitContainerTypes are all added to a byte array that will be sent to the interface via the standard message protocol TCP/IP. The data values can be larger than a byte or smaller than a byte. Because the BitContainerTypes are different lengths, each of the different sizes need to be handled. If the BitContainerType is the same size as a native type it's simple to convert the bits to the byte array. A BitOperations class was created to handle these conversions.

For example converting an integer to a byte array would use the following code from the BitOperations class:

public static byte[] intToByteArray(int myInteger) {
byte[] value = new byte[4];
value[0] = (byte) ((myInteger >> 24) & 0x000000FF);
value[1] = (byte) ((myInteger >> 16) & 0x000000FF);
value[2] = (byte) ((myInteger >> 8) & 0x000000FF);
value[3] = (byte) (myInteger & 0x00FF);
return value;

If the BitContainerType is not the same size as a native type, the conversion is more difficult. There are two distinct cases that must be handled. The first is a data object that is less than eight bits and the second is the case where the data object is greater than eight bits. In the latter, the object will "straddle" two or more bytes.

For example:

BitContainerType unused8  = new BitContainerType (4,3,0);
BitContainerType customerCode = new BitContainerType (6,5,42);
BitContainerType customerId = new BitContainerType,(6,5,55);

In this example, the first data value is four bits long, the second data value is six bits long and the final data value is six bits long. The interface is expecting a message with the following bits: 0000 1010 1011 0111.

To send 0000 1010 1011 0111, the first four bits must be added to the first byte in the byte array, the second data value, with a start bit location of four, must be added to the end of the first byte in the byte array and added to the first part of the second byte in the byte array, essentially straddling two bytes. Finally the last six bits, with start bit location 10, must be placed at the end of the second byte in the byte array. The bits will need to be shifted and masked to add them to the bytes correctly.

The sample code in Listing 3 handles both of these cases. In the case where the data object doesn't fill the entire byte, there's a check for the number of bits along with a variable that holds the location for the next bit and a variable to keep track of which byte the processing is in.

There's also a check to determine if the data object is greater than eight bits or overlaps onto a second byte. In this case, the byte array is incremented and the bits are added to the next byte in the byte array.

The final result is a byte array with the data in the correct order ready to be sent to the interface as APIs. The byte array is then sent to a message API that creates a byte buffer consisting of a predefined header and the body. The body consists of the byte array of data fields added to the byte buffer.

The creative solution of creating a BitContainerType class allows the user to convert the data fields into objects of variable length bits that can be accessed and processed efficiently. This lets the developer quickly and easily create formatted interface messages. It also lets the user be more efficient when it comes to data storage which is a plus for algorithmic-intensive scientific applications that require bitwise manipulations. While Java doesn't have a bit class, it's flexible enough to let the user create his own "bit type" and BitContainerType classes. I hope this article has convinced you that Java offers a solution for algorithmic-intensive scientific applications that require bitwise manipulations and is a good alternative to other languages.

More Stories By Nicole Redmond

Nicole Redmond has 14 years of software IT experience, including 9 years of Java development. She is currently developing enterprise applications that integrate COTS and legacy software with Java and web applications. Nicole is a senior member of the engineering staff with Lockheed Martin Corporation working out of Moorestown, NJ.

More Stories By David Dieterle

David Dieterle has 10 years' experience in Java and database development, with the last 8 years in enterprise software development. He is currently involved in numerous development projects focusing on integration of stove-piped solutions and web development. David is a senior member of the engineering staff with Lockheed Martin Corporation working out of Moorestown, NJ.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
sflanaga 05/26/09 11:02:00 PM EDT

I have a post concerning java on reading binary data using a C idiom that uses structs and bit-fields. I really do miss this idiom in java since it made this type of thing easy. However, instead of relying on a language builtins that the C bit field relies on, this uses java Annotations with reflection and could be extended for other strange file or network types. I have written how to read, but did not write up a "write" routine. The write could just as well be done. I thought it meshed well with this article. Take a look

@ThingsExpo Stories
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering Cloud Expo and @ThingsExpo will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at Cloud Expo. Product announcements during our show provide your company with the most reach through our targeted audiences.
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City. Digital Transformation (DX) is a major focus with the introduction of DXWorldEXPO within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of bus...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
DXWorldEXPO LLC announced today that "Miami Blockchain Event by FinTechEXPO" has announced that its Call for Papers is now open. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Financial enterprises in New York City, London, Singapore, and other world financial capitals are embracing a new generation of smart, automated FinTech that eliminates many cumbersome, slow, and expe...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER give you detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPO also offers s...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Predicting the future has never been more challenging - not because of the lack of data but because of the flood of ungoverned and risk laden information. Microsoft states that 2.5 exabytes of data are created every day. Expectations and reliance on data are being pushed to the limits, as demands around hybrid options continue to grow.
Poor data quality and analytics drive down business value. In fact, Gartner estimated that the average financial impact of poor data quality on organizations is $9.7 million per year. But bad data is much more than a cost center. By eroding trust in information, analytics and the business decisions based on these, it is a serious impediment to digital transformation.
Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...