Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BitSet to unsinged Int Java

Posted on 2009-04-06
7
Medium Priority
?
1,539 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
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 …
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…
Suggested Courses

660 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