participate


New To Java Technology Archive - Why is a boolean a byte and not a bit?
<<   Back to Forum  |   Give us Feedback
This topic has 35 replies on 3 pages.    1 | 2 | 3 | Next »
ffvanderlaan
Posts:34
Registered: 10/22/03
Why is a boolean a byte and not a bit?   
Oct 23, 2003 6:49 AM

 
Now that I've got this account I might as well use it... :)

Why is the size of a boolean not a bit?
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 6:52 AM (reply 1 of 35)  (In reply to original post )

 
Why is the size of a boolean not a bit?

How do you know it isn't?
 
CeciNEstPasUnProgrammeur
Posts:31,612
Registered: 7/23/02
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 6:53 AM (reply 2 of 35)  (In reply to original post )

 
Why is the size of a boolean not a bit?

Never read the specs, but IIRC, it's a byte because you can't address a bit in memory. Though multiple booleans are in fact put togther into one byte to save space.
 
ffvanderlaan
Posts:34
Registered: 10/22/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:06 AM (reply 3 of 35)  (In reply to #2 )

 
hmmm... I have an array with 4k booleans, it's 4kb in memory.
 
plumone
Posts:402
Registered: 7/17/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:13 AM (reply 4 of 35)  (In reply to #3 )

 
hmmm... I have an array with 4k booleans, it's 4kb in
memory.

the size is left to the writers of the particular JVM. It wouldn't be that tough to pack a byte but on the other hand it might be a slowdown.

Getting the exact size will be tough though. there are other factors to consider if you want to get to the byte level. for example, do you know the state of the stack just prior and just after your memory allocation size check? Do you know whats happening out in the heap with other threads allocating and deallocating memory?

How do you know for certain that part of what you are seeing isn't a side effect of a different thread within that particular jvm?

But if it really concerns you then make your own booleans bit sized (by declaring a short, and then packing/testing the bits yourself). Nothing stopping you from doing that.

 
andrew_malcolm
Posts:2,120
Registered: 11/12/01
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:15 AM (reply 5 of 35)  (In reply to #4 )

 
But if it really concerns you then make your own booleans bit sized (by declaring a short, and then packing/testing the bits yourself). Nothing stopping you from doing that.

Or you could use java.util.BitSet
 
ffvanderlaan
Posts:34
Registered: 10/22/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:18 AM (reply 6 of 35)  (In reply to #4 )

 
In j2me:

long freemem;
System.gc();
freemem = Runtime.getRuntime().freeMemory();
boolean[][] path = new boolean[main.mapSize][main.mapSize];
System.gc();
freemem = (freemem-Runtime.getRuntime().freeMemory());
return freemem;

... does the trick pretty much. There aren't much threads running anyways. I don't have much memory on my phone and my map is pretty big, but I don't have much speed eather so I'll find another solution. Thanx 4 your reply.
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:20 AM (reply 7 of 35)  (In reply to #3 )

 
hmmm... I have an array with 4k booleans, it's 4kb in memory.

This implementation will give you the fastest possible access to each boolean. You can trade the fast access against a denser storage using a little bit-fiddling. Make it an int array and store 32 booleans as bits in each integer. The size of the array will shrink 32 times. You can also have a look at BitSet. I think it uses a dense memory representation but I'm not sure.
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:22 AM (reply 8 of 35)  (In reply to #7 )

 
Well, as armalcolm just said.
 
silk.m
Posts:2,829
Registered: 9/1/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:41 AM (reply 9 of 35)  (In reply to #2 )

 
Why is the size of a boolean not a bit?

Never read the specs, but IIRC, it's a byte because
you can't address a bit in memory.

what exactly do you mean by that ... ?

you can access byte via:
cmp al, number


and access bit via:
and eax, bit


... ?
 
YoGee
Posts:4,760
Registered: 6/19/02
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 7:52 AM (reply 10 of 35)  (In reply to #9 )

 
There is actually no boolean type. A Java expression that operates on boolean values is compiled to use the int data type to represent boolean variables. This means a boolean is usually a 32-bit variable. However arrays of booleans are treated as arrays of bytes, so booleans are 8 bits in an array.
 
sethaw
Posts:303
Registered: 9/30/02
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 8:16 AM (reply 11 of 35)  (In reply to #9 )

 
Never read the specs, but IIRC, it's a byte because
you can't address a bit in memory.

what exactly do you mean by that ... ?

This is hardware dependent. In some architectures you can't access one byte at a time either, you have to get larger chunks of memory each read.
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 8:17 AM (reply 12 of 35)  (In reply to #10 )

 
There is actually no boolean type.

This really comes as a chock to me. When did they disappear?
 
andrew_malcolm
Posts:2,120
Registered: 11/12/01
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 8:21 AM (reply 13 of 35)  (In reply to #12 )

 
There is actually no boolean type.

I think he mean in the underlying hardware, and in a sense on x86 architectures that's true, but you can access the bits within bytes/words/dwords.

Some processors (such as the 8051) do have bit-addressable memory.
 
sethaw
Posts:303
Registered: 9/30/02
Re: Why is a boolean a byte and not a bit?   
Oct 23, 2003 8:32 AM (reply 14 of 35)  (In reply to #12 )

 
There is actually no boolean type.

This really comes as a chock to me. When did they
disappear?

If you look at the bytecode, you can see java handles ints and booleans the same.
{
   int x = 0;
   boolean b = false;   
}
with javac this compiles to:
   0 iconst_0
   1 istore_1
   2 iconst_0
   3 istore_2


The i at the beginning of the instructions stands for integer, so obviously its treating it as an integer. Its even loading the same value into the local variable like it handles strings that are the same.
 
This topic has 35 replies on 3 pages.    1 | 2 | 3 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 59
  • Guests : 125

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums