Solved

BitSet to unsinged Int Java

Posted on 2009-04-06
7
1,496 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 48
custom annotations 9 30
replace a word with other 1 40
servlet example issue 6 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now