473,499 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boolean array to integer?

10 New Member
Suppose I have a boolean array:
Expand|Select|Wrap|Line Numbers
  1.  
  2. boolean[] b = {true, true, false, false, true, true};
  3.  
I want to convert it to a decimal number like a binary number. I this case the array represents number 110011 in decimal. So how do I convert the array to binary and then integer?
May 2 '07 #1
5 15041
JosAH
11,448 Recognized Expert MVP
Suppose I have a boolean array:
Expand|Select|Wrap|Line Numbers
  1.  
  2. boolean[] b = {true, true, false, false, true, true};
  3.  
I want to convert it to a decimal number like a binary number. I this case the array represents number 110011 in decimal. So how do I convert the array to binary and then integer?
Hint, for the following numbers: 0, 1, 2, ... n think of this sequence instead:
1<<0, 1<<1, 1<<2 , ... 1<<n

kind regards,

Jos
May 2 '07 #2
Manticure
10 New Member
I'm sorry, I don't know yet about the shift operators, what they do or what they are for. Can you please explain more?
May 2 '07 #3
merinodaniel
5 New Member
ok, this is what i think u want to do, u want to have a array with integers like bynary so when u have true is 1 and false is 0.
well i can do this:

boolean[] b = {true, true, false, false, true, true};
int[] a = new int[b.lenght];
for(int i = 0; i <= b.lenght; i++){
if(b[i].equals(true)){
a[i] = 1;
}else{
a[i] = 0;
}
}

i think this is ok, sry if something wrong but i just delent de jDeveloper :P so i dont know if this java code its ok
PM if something wrong
bye
May 3 '07 #4
JosAH
11,448 Recognized Expert MVP
I'm sorry, I don't know yet about the shift operators, what they do or what they are for. Can you please explain more?
Ok, I'll rewrite my hint for you then: for the following numbers: 0, 1, 2, 3 ... n think
of this sequence instead: 2**0, 2**1, 2**2, 2**3, ... 2**n where '**' is my
notation for "raised to the power" (note '**' is not a Java operator).

kind regards,

Jos
May 3 '07 #5
prometheuzz
197 Recognized Expert New Member
ok, this is what i think u want to do, u want to have a array with integers like bynary so when u have true is 1 and false is 0.
well i can do this:

boolean[] b = {true, true, false, false, true, true};
int[] a = new int[b.lenght];
for(int i = 0; i <= b.lenght; i++){
if(b[i].equals(true)){
a[i] = 1;
}else{
a[i] = 0;
}
}

i think this is ok, sry if something wrong but i just delent de jDeveloper :P so i dont know if this java code its ok
PM if something wrong
bye
No, that is not OK. The OP does not want the binary notation, but rather the decimal value. And if(b[i].equals(true)){ will not compile, nor will b.lenght.
May 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1553
by: Shalini | last post by:
Hi , Is the code below efficient?? bool *isElement(char **a1,long lengtha1,char **a2,long lengtha2){ charQuickSort(a2, 0, (lengtha2-1)); bool *tag=(bool*)calloc(lengtha1,sizeof(bool)); int...
14
29673
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
2
1841
by: Len via DotNetMonster.com | last post by:
I have to set up a 2d array(13, 4) containing a deck of playing cards. What I am wondering is how to correctly set up the array in the first place, and then how do you access the information once...
11
10263
by: Jason Heyes | last post by:
What STL algorithm do I use to set all bool elements of an array to false in just one line of code? Thanks!
2
4272
by: Speed | last post by:
Could you please tell me which is the most efficient of way writing a BOOLEAN array to a file. Thanks a ton. Speed
2
2293
by: Speed | last post by:
Could you please tell me which is the most efficient of way writing a BOOLEAN array to a file. Thanks a ton. Speed
4
2605
by: Learner | last post by:
Hello, I have database field called 'PullAHead' defined as a bit field. Now if user doesn't pick a 'Yes' or 'No' in the front I need to be able to send a null value into the 'PullAHead' field in...
3
2253
by: The Midnighter | last post by:
Alright, so basically here's what's supposed to happen. It's an Airline reservation system, basically taking a row of 10 seats, 1 isle, 1 window. Yes, there's only one row. xD So 5 seats isle, 5...
2
2554
Mary Seelye
by: Mary Seelye | last post by:
I don't even know where to start with this or figure out how to get them to display. I need the first 25 to be true and the last 25 to be false. I need all the trues and falses to display.
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.