wood burning stoves*
The moose likes Beginning Java and the fly likes Beginner question about bit conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner question about bit conversion" Watch "Beginner question about bit conversion" New topic
Author

Beginner question about bit conversion

Alan Christen
Greenhorn

Joined: Sep 18, 2006
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??

Thanks in advance for your time.

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

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Christen
Greenhorn

Joined: Sep 18, 2006
Posts: 16
Thank you very much for your reply!!! It helped!

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

Thank in advance.

Al.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Wikipedia has a small article about it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Beginner question about bit conversion
 
Similar Threads
Conversion of Primitives
newbie question re: "design patterns" (?)
Need pointers regarding web service
Populating hashmap does not work
view ddl for all triggers