Solved

BitSet to unsinged Int Java

Posted on 2009-04-06
7
1,517 Views
Last Modified: 2012-05-06
Hi i have a BitSet of 8 bits which are set with 8 boolean values. now how to convert this 8 bits to unsinged int or char in java.
public static BitSet bitSets(boolean[] vals)
		{
			
			BitSet bits1 = new BitSet(8); 
			for(int i=0; i<vals.length; i++) { 
			   bits1.set(i,vals[i]); 
			 }
			System.out.println("Bitset length--->"+bits1.length());
			return bits1;
	    }

Open in new window

0
Comment
Question by:karpra
  • 3
  • 3
7 Comments
 

Author Comment

by:karpra
ID: 24081991
the array can be a boolean value of 0 or 1 or integer value or 0 or 1 and the BitSet looks like 10010111 now this value of BitSets should be represented as unsinged int or char
thanks in advance
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24082181
Try
        public static int bitset2int(BitSet bs) {
                int result = 0;
                for(int i = 0;i < bs.length();i++) {
                    if (bs.get(i)) {
                        result |= (1 << i); 
                    }
                }
                return result & Integer.MAX_VALUE;
        }

Open in new window

0
 

Author Comment

by:karpra
ID: 24082872
Thanks man
Can i use this below if i want 8 bit char actuall i need both. just fine tuned your method above.
thanks in advance,
Please correct if there is any needed.
public static char bitset2char(BitSet bs) {
              char result = 0;
              for(int i = 0;i < bs.length();i++) {
                  if (bs.get(i)) {
                      result |= (1 << i);
                  }
              }
              return (char) (result & Character.MAX_VALUE);
          }
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 24085177
Since char is unsigned anyway, you only need to do make one change:
return (char) result;

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24096143
Any further questions?
0
 

Author Comment

by:karpra
ID: 24099482
oh just wating for testing results from the test team..:) will confirm.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
configure dependency in POM for new database 3 34
servlet example 17 52
Need Help! Getting a syntax error and don't understand why 3 31
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question