Re: Boolean Variable Type
- Posted by Steve Mosher <farq at KILN.ISN.NET> Feb 17, 2000
- 552 views
On Thu, 17 Feb 2000, Brian Broker wrote: > On Thu, 17 Feb 2000 21:21:54 GMT, Lewis Townsend wrote: > > >But I don't really care if it > >takes up 32 bytes of space or not. > > I think would really concern me if it took up 32 bytes of space... unless > you meant to say 32 bits (4 bytes). 8^) > > But I'd have to agree that the memory 'wasted' on storing flags in an > integer is trivial these days. And I would bet that there would be a > performance hit from the need to (internally) mask a bit to determine a > boolean value. > > -- Brian > You'd better believe it. The minimum a CPU can write to memory is 8 bits. Most C programs use 32-bit integers for flags. They should be using 8-bit ints, but rarely do. The problem with a data type of only one bit is that it takes far more to address it than it contains. Furthermore, the only thing you can put in those remaining seven bits are other 1 bit types. If you've only got 1 flag in your programn, a boolean type will save you no memory (it will actually waste some, address & bit vs. address). 32 bits will address (by byte) 4,294,967,296 bytes. To do this by bit, you need 35 bits, three bits wasted per address. Then there's the overhead involved in translating the address into a readable form, and back. Generally, there's no real advantage. To demonstrate this, think about writing flag handling routines in euphoria. You'll notice that no matter how you do it, it will waste both memory and speed. Same deal if it were compiled into the language.