Bit patterns in Java

How do I create a bit pattern (bit string, bit stream?) in Java?
What I'd like to do is to contract data and use a bit pattern instead of the Java built-in standard classes. With what Java tools/classes can I achieve this?
Example: Storing personal data for a high school student:
Bit 1 (nationality): American=0, Foreigner=1
Bit 2 (sex): male=0, female=1
Bit 3-4 (grade/class): freshman=00, sophomore=01, junior=10, senior=11
And so on...
The bit pattern is supposed to be able to hold a lot of information, at least a 100 bits or so.
Thanks!
/peso

peso wrote:
Thanks for your reply JoachimSauer. You're right in what you're saying that space is cheap and all, but in this case it's essential for my application that I store the information in as few bits as possible.Why is that, I wonder? Out of curiosity. Are you storing/transfering so many records or is the space/bandwidth so limited?
And I'm also interested in learning the best way to create and build a bit pattern with the information I have.
Say I have about a hundred different strings containing different information:
String nationality = "American";
String sex = "Male";
String grade = "Sophomore";
String age = "17";
Etc...
I could store this as for example "American;Male;Sophomore;17".The first step would be to get rid of any Strings and transfer that to some better-fitting datatypes. For example create an enum for nationality (even if it only got the values AMERICAN and FOREIGN, I wouldn't call that nationality, btw).
Then an enum for the sex and grade. And use an int to store the age.
Because using Strings for things which aren't arbitary (or at least somewhat freeform) character strings is ... wrong.
But what I'd like to do is to encode this information in a bit pattern like for example "0 0 01 10001" and then converting the bit pattern into a string of ASCII characters by taking lets say 5 bits at a time. Say the beginning of my bit pattern would look like "01011 00101 11001 11110 01000". That would produce the following string with ASCII characters (if I add 33 to the value in order to get a value which represents a writable ASCII character) ",&:?)".I'd use the Base64 encoding, if an ASCII-representation was absolutely needed. But if you really need to use as little space as possible, then you should keep a binary format.
So the question is, what's the best way to build my bit pattern? Say I need 124 bits for all my information. Do I create a byte array of size 16 and manipulate each byte by shifting and masking? Or is there another way to do it?Use a BitSet.

Similar Messages

  • Bit pattern, again

    I want to get to know the bit pattern of a double or float variable.
    Originally I thought that I can use Double.doubleToLongBits() or Double.doubleToRawLongBits() to return a long variable and then Long.toBinaryString() to see the bit pattern.
    But the bit pattern didn't make perfect sense when compared to the IEEE specs for floating point arithmetic.
    The problem appears to be that - according to the Javadoc - there is no guarantee that doubleToLongBits() and the other one return a value that really reflects the bit pattern completely. The only guarantee there is - apparently - that the long value returned will produce the same double value again when it is passed to Double.longBitsToDouble() . But that is not a guarantee that the bit pattern of the long variable returned really reflects the bit pattern of the double. And in reality it apparently doesn't.
    Looking at the source of doubleToLongBits() isn't possible, because both are native methods.
    Am I making myself clear? If not, never mind. The important thing now is:
    I'm back at bit shifting. But I can't do a bitwise & between a double and a long. So, this doesn't work:
    public class DoubleBits2 {
        public static void main(String[] args) {
            double d = 1.0;
            long val = 0x8000000000000000L;
            if ( (d & val) == val){
                System.out.print( '1' );
            else {
                System.out.print( '0' );
            val = 0x4000000000000000L;
            for ( int i = 1; i < 63; i++ ){
                if ( ( d & val ) == val ){
                    System.out.print( '1' );
                else {
                    System.out.print( '0' );
    }Any advice?
    It never hurts to ask. :-)

    >
    The problem appears to be that - according to the
    Javadoc - there is no guarantee that
    doubleToLongBits() and the other one return a
    value that really reflects the bit pattern
    completely. The only guarantee there is - apparently
    - that the long value returned will produce
    the same double value again when it is passed
    to Double.longBitsToDouble() . But that is not
    a guarantee that the bit pattern of the long
    variable returned really reflects the bit pattern of
    the double. And in reality it apparently
    doesn't.I have tranferred IEEE values from C to Java anc Java to C using doubleToLong().
    Looking at the 1.5 Javadoc I don't see where it says it does not return the IEEE format -
    public static long doubleToLongBits(double value)
    Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.
    Bit 63 (the bit that is selected by the mask 0x8000000000000000L) represents the sign of the floating-point number. Bits 62-52 (the bits that are selected by the mask 0x7ff0000000000000L) represent the exponent. Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL) represent the significand (sometimes called the mantissa) of the floating-point number.
    If the argument is positive infinity, the result is 0x7ff0000000000000L.
    If the argument is negative infinity, the result is 0xfff0000000000000L.
    If the argument is NaN, the result is 0x7ff8000000000000L.
    In all cases, the result is a long integer that, when given to the longBitsToDouble(long) method, will produce a floating-point value the same as the argument to doubleToLongBits (except all NaN values are collapsed to a single "canonical" NaN value).

  • High frequency bit pattern evaluation

    I am trying to use a 6601 counter/timer to count the bits in a high speed serial output from a linear encoder.  I've successfully used the counter/timer card to clock and reset the encoder, but I'm having trouble using my third counter input to correctly tell me where the edges of the bit pattern are.  I realise I should probably have bought a serial card to do the job properly, but what the heck ;-).
    I'm using Borland C++Builder, by the way.
    I aimed to use the clock signal to latch the buffer and record a one or a zero in my edge count array to work out whether or not the data signal had changed state.
    The signal relationships are thus:
    I generate a finite pulse train 24 pulses long at a frequency up to 2MHz on CTR0. This output has a retriggerable start trigger on its CTR0GATE, which is generated at CTR2 (which I have to use instead of CTR1 as CTR1 seems to be affected by what is happening on CTR0 in some way that I do not fully understand). 
    The CTR0 output is sent to the transducer, which returns a serial bit pattern starting with the most significant bit and ending with the least significant bit.  CTR0 is high at idle as this is what the transducer requires to reset to its first bit.  If there is no pause at the end of the clock train the encoder returns the same value irrespective of where it actually is (like a sort of sample/hold). 
    The data return from the transducer comes in on CTR3SOURCE, which uses the CTR0 clock coming in on CTR3GATE to latch the buffer and reload the counter.
    CTR2 is a free running infinite pulse train that is usually low but goes high shortly after the end of the pulse train on CTR0 to retrigger CTR0 as described above.
    There is some crosstalk on the transducer output from the clock signal, but since I am having trouble reading back the edge count array I can't really tell if it is causing any problems.  However, I do know that the linear encoder is behaving as expected.
    Thanks for any ideas anyone might have!
    Des

    Hi,
    Thanks for posting the problem on the NI forum.
    You wrote:
    >I generate a finite pulse train 24 pulses long at a frequency up to 2MHz on CTR0. This output has a retriggerable start trigger on its >CTR0GATE, which is generated at CTR2 (which I have to use instead of CTR1 as CTR1 seems to be affected by what is happening on
    >CTR0 in some way that I do not fully understand). 
    Essentially when generating a finite pulse the counter channels are paired up i.e. it uses the gate to give the counter pulse out on CTR2. In other words it is internally routed in the maner you have describe hence its not an unexpected behaviour. CTR 1 is used internally to genetate the pulse at CTR 2.
    I was wondering whether you could perhaps provide a timing diagram of the counter pulses recieved.
    Kind Regards,
    Kurt

  • How to count bit patterns in a file ?

    Think of the following:
    You have two files that you want to compare bit by bit.
    A file has been send the other one has been received.
    The two files can be of different size (Connection lost).
    But you not only want to know how many bits are different you want also to know the run length of the different bits.
    For example:
    File 1:                      1010101011101010111101011001
    File 2:                      1011101010001101010111010101
    What is the result ?
    Xor File1 and File2:   0001000001100111101010001100
    Run Length 1:           3
    Run Length 2:           2
    Run Length 3:           0
    Run Length 4:           1
    I hope I didn't make a mistake.
    Think also of using a blocksize for comparison.
    Maybe you don't want to compare the files as a whole but want to know the number of errors for every block that is maybe 256 bit or 1024 bit. So read the files block by block, xor the blocks and count the bit errors by their run length.
    So how can I get this result in LabView ?
    Which data types do I have to take for comparison ? Which VIs do I have to use ?
    Thanx for help and your time

    Hi Allen,
    thanks for your posting.
    I have now a working version.
    For my as LabView newbee this was really hard work.
    So how many bit patterns of "1" or "11" or "111" or "1111" are in the xor result of the two files ?
    Or you can also ask: How long is the error (Burst error) ? And how often does it occur ?
    Look for example for block error codes and this stuff.
    Attached is the VI and two small text files (Both 10 Bytes).
    I've tried a blocksize of 10 Bytes and also 5 Bytes.
    The result is:
    1     2     3      4
    16   5     1      1
    So there 16 errors of run length 1, 5 of run length 2 and so on.
    Would be also nice to count special bit patterns like '1001101010111" or something like that.
    But I think this is more easy.
    Not the pattern is searched. The first 1 and the following 0 is searched.
    The run length is used as index.
    Attachments:
    Filediff 4.vi ‏32 KB
    Kopie1 von REAMDE.txt ‏1 KB
    Kopie2 von REAMDE.txt ‏1 KB

  • I am new to this and a bit rusty with Java, Oracle, Web Development, et al; and, I need a free trial run at this time, Can we do this?

    I am new to this and a bit rusty with Java, Oracle, Web Development, et al; and, I need a free trial run at this time, Can we do this?

    your itunes accoutn have nothing to d with your computer
    you can go to any computer with internet access in the world and
    login into your itunes account if you have the appleID and password
    the itunes account is placed on apples itunes servers

  • 32-bit JVM receiving Java objects from 64-bit JVM

    Hi folks,
    Question is: will there be problems for 32-bit JVM receiving Java objects from 64-bit JVM? and vice versa.
    Our application client is running on 32-bit JVM, our server is running on 64-bit JVM. Client will send Java objects to server, and vice versa.
    My past experience suggested when sending Java objects between client and server, both client and server needs to be compiled under the same JVM version. Any advice?
    Christy

    My past experience suggested when sending Java
    objects between client and server, both client and
    server needs to be compiled under the same JVM
    version. Any advice?This is only a case if you omit explicit serialVersionUID. My advice is to ALWAYS specify it for classes you want to serialize over the wire or put into persistent storage. It is way too tricky to rely on default one to fail half a year later when some new programmer adds one new public method to a class.
    Unless you need to deserialize already existing resources, there is no need to put any magic number in serialVersionUID - just put 1 for every class you create and possibly increase it by 1 every time you want to make incompatible version (which is not happening so often, as in real world you often try to stay as compatible as possible)

  • Boot failure - white screen corrupted with yellow repeated bit-pattern.

    Hi
    I have had a quick look at the postings esp those with pictures and can't find a similar prob to mine. My white 24" iMac hasn't been able to boot for months now, except a couple of times but it then subsequently started playing up. As well as a boot failure the white boot screen has yellow bit-patterns on it, usually horizontal or speckled. I have been able to run the diagnostics program from the OSX disc but it didn't find any problems. The computer displays and animates the spinning wheel while booting, but overlaid on the corrupted white screen, and then 99% of time gets no further.
    I have made 5 screenshots with my 3G iPhone, the first two are of one boot attempt, the third is another boot attempt (note slightly different bit pattern - this one is the most common I get) and last two are of a successful boot attempt into AHT (Apple Hardware Test). If anyone recognizes this type of screen problem please do reply.
    Standard boot attempt (1) - two photos showing spinning wheel:
    https://files.me.com/funkypuck/e0nt8c
    https://files.me.com/funkypuck/nts1nu
    Standard boot attempt (2) - one photo showing typical screen corruption:
    https://files.me.com/funkypuck/izjv0e
    Boot into Apple Hardware Test - two photos of same area (just because 3G camera not so hot):
    https://files.me.com/funkypuck/q64kgt
    https://files.me.com/funkypuck/e72fkd
    Many thanks

    Hi, I am having a similar problem from what you described, but I can't even get the disk up and running for a clean install with Snow Leopard.  I'm curious if you have found a solution.  I fairly certain that it is not an issue with the hard drive. 

  • Bit new to Java.

    I'm a bit new to Java. Can someone pls tell me if this: class hello extends JComponent and class hey extends JComponent are kinds of inheritance. Thanks a lot.

    Yes, anytime you use 'extends', you've created an inheritance structure. In the two lines you posted, both 'hello' and 'hey' can be used as a JComponent, and have it's member variables and methods.

  • Desperately trying to find patterns with Java code samples

    Greetings. Is there anyone who knows where I can find Java patterns, I am particularly looking for template, literator, composite, observer and decorator. I have gone to Hillside and Portland Repository sites, but did not find pattern sample code.
    I bought the head first Java book which I love, but I would like to see more sample code. They mention pattern catalogues, and besides the Gof I cannot find any. I really want to look at loads of actual samples to get a real feel how these patterns can be applied. Where can I go please?
    Thank you for any and all help.
    Peace,

    You're most welcome - you might check out the Head
    First Design Patterns book as well.Yes, that was fun to read!

  • Design Patterns for java swing application front a J2EE server

    Hi,
    i dont nkow that i stay in the correct forum. I am development a java swing client application, no web, and i dont know that if there are any patterns for a client of this type.
    I have readed http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
    but after of Buissnes Delegate pattern, the others one, over ejb tier, are applicated only for web clients, using servlets and jsp pages.
    If i have a swing client application, what patterns i must folow for implement the client logic of my swing application??
    thanks

    MVC pattern is one of the most used
    http://en.wikipedia.org/wiki/MVC
    http://csis.pace.edu/~bergin/mvc/mvcgui.html
    ...

  • DAO pattern and Java Persistence API

    Hi
    This is a question for anyone who might be familiar with the standard DAO design pattern and the Java Persistence API (JPA - part of EJB3). I'm new to this technology, so apologies for any terminology aberrations.
    I am developing the overall architecture for an enterprise system. I intend to use the DAO pattern as the conceptual basis for all data access - this data will reside in a number of forms (e.g. RDBMS, flat file). In the specific case of the RDBMS, I intend to use JPA. My understanding of JPA is that it does/can support the DAO concept, but I'm struggling to get my head around how the two ideas (can be made to) relate to each other.
    For example, the DAO pattern is all about how business objects, data access objects, data transfer objects, data sources, etc relate to each other; JPA is all about entities and persistence units/contexts relate to each other. Further, JPA uses ORM, which is not a DAO concept.
    So, to summarise - can DAO and JPA work together and if so how?
    Thanks
    P.S. Please let me know if you think this topic would be more visible in another forum (e.g. EJB).

    Thanks, duffymo, that makes sense. However ... having read through numerous threads in which you voice your opinion of the DAO World According to Sun, I'd be interested to know your thoughts on the following ...
    Basically, I'm in the process of proposing an enterprise system architecture, which will use DAO as the primary persistence abstraction, and DAO + JPA in the particular case of persistence to a RDBMS. In doing so, I'd like to illustrate the various elements of the DAO pattern, a la the standard class diagram that relates BusinessObject / DataAccessObject / DataSource / TransferObject (http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html). With reference to this model, I know that you have a view on the concept of TransferObject (aka ValueObject?) - how would you depict the DAO pattern in its most generic form? Or is the concept of a generic DAO pattern compromised by the specific implementation that is used (in this case JPA)?

  • How to apply patterns in Java desktop system

    Dear experts,
    I have developed a System which now only can be use through Browser, in other words,a WEB application for the System has developed which I use MVC pattern in it
    now,I should develop a client software for some local customers to use system,but I have not any experiences in developing client desktop software,the main problem now is that I don't know which patterns to use?
    Can you help me ? thanks!!!

    Hi,
    Implement the same MVC arch, but the client side which I suppose you are going to develope in Java AWT/Swing should call the controller (Servlet) using the java.net packages. This works very fine but needs some small hardships....
    jHeaven

  • GOF Patterns in Java

    I am currently reading the GOF book 'Design Patterns'.
    I was wondering if anyone could point me to a good book,
    or online link where GOF patterns are identified in the
    J2SE/J2EE packages?
    For example, the Decorator pattern appears to form the basis
    of the java.io package?
    Iterator is obviously the same, except the C++ examples benefit
    from using templates, so that the resulting Iterators are type-safe
    (coming soon with Generics?).
    And the Flyweight pattern appears to have some relation to EJB,
    where extrinsic state is loaded/unloaded using ejbLoad/ejbStore
    methods?
    Thanks, Neil

    oops - didn't read your entire post, sorry.
    Check out
    http://java.sun.com/blueprints/corej2eepatterns/index.html
    For patterns related to J2EE development - not sure where you can find same for J2SE, but you seem to have a pretty good handle on that anyway.
    Good Luck
    Lee

  • How do I synchronize two 6534 devices to output 45 bit pattern?

    I am using a PXI chassis with two 6534 cards. I am generating a 45 bit wide data pattern and sending 32 bits per update to one device buffer and 13 bits to the other. However my trigger is an external clock that is always running. This means that the data is sent to the outputs as soon as it arrives at the device buffer. On a logic analyser i've seen that the data is not in sync. I therefore need to make sure that the data on each channel is synchronized. How can this be done?

    Coxy
    Is it that you want to make sure the two 6534 moduals are in sync sending out the data? If this is the case, and you're doing pattern I/O, you can use the same clock for both moduals. (This can be done internally routing one clock to the a RTSI line.) Let me know if I am understanding your application wrong.
    Brian

  • Fresh Bits! Oracle Java ME Embedded and ME SDK updates

    Oracle has just refreshed the Java ME Embedded 3.3 EA and ME SDK 3.3 EA binaries.
    For more information, please see my blog: https://terrencebarr.wordpress.com/2013/06/13/fresh-bits-oracle-java-me-embedded-and-me-sdk-updates/
    Best regards,
    Terrence Barr
    Principal Product Manager, Oracle

    Hello,
    I am just viewing this course now and am stuck on finding the raspberry pi 8.1 EA binary:
    oracle-jmee-8-1-ea-raspberrypi-linux-bin-b01-20_may_2014.zip
    The only thing I can find on this page (which is what the document your link brings up has)
    http://www.oracle.com/technetwork/java/embedded/javame/embed-me/downloads/index.html
    is 8.0:
    oracle-jmee-8-0-rr-raspberrypi-linux-bin.zip
    Please help...
    Thanks

Maybe you are looking for

  • System Preferences Hang-up

    Hi, I am having a problem with System Preferences freezing up when selecting Desktop & Screen Saver. Everything else on the system works fine. I loaded a picture on the Macintosh HD Device (all other pictures are under a specific user account) to acc

  • Anyone ever created an RSS feed in DW?

    Take a look at the scrolling ticker at www.londonstockexchange.com top of the homepage I want to create something similar to this at www.investix.co.uk to fit just snug under the horizontal nav menu bar near the top. Does this involve setting up an R

  • How set variable in Terminal (once)

    In a Termina lsession, I want to create a new variable, MLROOT, and assign it the value of a path: /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/CompilerAd ditions I've tried the following,and just get a "not a valid identifie

  • Macbook Pro Late 2008 usb to serial cable stops communicating after Yosemite upgrade

    I recently updated my MacbookPro to Yosemite and have found that my usb to serial cable no longer works. I've tried reinstalled drivers multiple times with no success when I connect my Gigaware usb to serial cable and conect my cisco console cable wh

  • FB doesn't recognize variables just declared

    Hi Here's the code in an <fx:Script> tag: var myXML:XML; var req:URLRequest = new URLRequest("photos.xml"); var loader:URLLoader = new URLLoader(); loader.load(req); loader.addEventListener(Event.COMPLETE, dataLoaded); function dataLoaded(evtObj:Even