Solved

BitSet to unsinged Int Java

Posted on 2009-04-06
7
1,533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

617 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