Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Beginner question about bit conversion

Alan Christen
Greenhorn
Posts: 16
Hello!

/****************DISCLAIMER****************/
I am a self-taught, beginning programmer with Java. So what, you asked? Well, there is a *lot* of the "basics" that I don't have...lots of things that aren't so obvious to me.
/****************DISCLAIMER****************/

I have a Oracle (10G) database table that contains 2 columns: callbackday (Date datatype) & callbacktime (Number(5) datatype).

My job is to extract data from this table and massage it before dumping it to another table. So far, I created a simple class that selects from this table and outputs the results to the console.

I was informed today that the "callbacktime" value is in fact a time value (HH:MI:SS). The explanation I was given is as follows:

This "callbacktime" field contains two-byte bitmap with the following format:

Bits 15-11 : Hour
Bits 10-5 : Minutes
Bits 4-0 : Seconds/2

How do you suggest I perform the conversion? I have started reading up on bitwise shift operators but I am not sure I am on the right path...

Can someone please provide a few pointers??

Al.

[ May 15, 2008: Message edited by: Alan Christen ]

[ May 15, 2008: Message edited by: Alan Christen ]

[ May 15, 2008: Message edited by: Alan Christen ]
[ May 15, 2008: Message edited by: Alan Christen ]

Rob Spoor
Sheriff
Posts: 20709
68
There are three shift operators:
- << will throw away the most significant bit (the sign bit), shift all other bits one to the left, and turn the least significant bit into a 0
- >>> works the other way around
- >> works as >>> but the most significant bit will be copied each time

In your case, it would be something like this, assuming that bit 0 is the least significant (right) bit:

Explanation:
1) 0x1F is a bitmask for 0000 0000 0000 0000 0000 0000 0001 1111. & will take both bitmasks, and return the bitmask with 1 only if both other bitmasks have 1. In the example, it will ignore all but the last 5 bits - bits 0-4.

2) Throws away the last 5 bits (just read), and fill the left with zeros.

3) 0x3F is a bitmask for 0000 0000 0000 0000 0000 0000 0011 1111. In the example, it will therefore ignore all but the last 6 bits - bits 5-10.

4) Throws away another 6 bits.

5) 0x1F is a bitmask for 0000 0000 0000 0000 0000 0000 0001 1111. In the example, it will therefore ignore all but the last 5 bits - bits 11-15.

Please note that I have used a short for the entire value; that is because it is a 16 bit number, whereas int is 32 bit. Since your number is only 15 bit there is no need for an int.

Now if the bits are the other way around, I'm sure you'll be able to find out how to extract the numbers.

Alan Christen
Greenhorn
Posts: 16

Can you please suggest books/website where I could find more on the topic of shift operators??

Al.

Rob Spoor
Sheriff
Posts: 20709
68
Wikipedia has a small article about it.

Campbell Ritchie
Sheriff
Posts: 50702
83
The Deitel books about Java have shoft operators in, usually as an appendix, and in the more recent additions this has been relegated to the accompanying CD.

There is only a brief article in the Java Tutorials.
This "lecture note" looks good; note it has links to the Java Language Specification, and the "bitwise" link at the bottom will tell you about the ~|^ and & operators (I epect).