Results 1 to 7 of 7

Thread: Bits, Bytes and Variables

  1. #1

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Talking

    Since I now know how (wow how many -ow ) bits and bytes work, I want to know how many bits (or bytes, I don't care) VB reserves for a variable.

    I mean how many bits/bytes does a string/integer/long/double/single/etc. use in the memory.
    And how many numbers can be in a Integer/long/double/single/etc. so I know wich variable to use for wich sort of number.

    I know for example
    101 in binary = 5 (1+0+4) in decimal, but how does it work with for example 5.5? Does the computer use a few bytes for a number with decimals?
    Like (0000010100000101) would be 5.5
    But how does the computer know if it's a decimal or not?

    Code:
    _________   ________
    |00000101|  |00000101|
    |^integer|  |^decimal|
    That'll clear some thinks up for me, thanx!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    From the VB help file;

    Code:
    The following table shows the supported data types, including storage sizes and ranges.
    
    Data type 	Storage size	Range
    Byte	1 byte	0 to 255
    Boolean	2 bytes	True or False
    Integer 	2 bytes	-32,768 to 32,767
    Long
    (long integer)	4 bytes	-2,147,483,648 to 2,147,483,647
    Single 
    (single-precision floating-point)	4 bytes	-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
    Double 
    (double-precision floating-point)	8 bytes	-1.79769313486232E308 to 
    -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
    Currency 
    (scaled integer)	8 bytes	-922,337,203,685,477.5808 to 922,337,203,685,477.5807
    Decimal	14 bytes	+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; 
    +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is 
    +/-0.0000000000000000000000000001
    Date	8 bytes	January 1, 100 to December 31, 9999
    Object	4 bytes	Any Object reference
    String 
    (variable-length)	10 bytes + string length	0 to approximately 2 billion 
    String
    (fixed-length)	Length of string	1 to approximately 65,400
    Variant
    (with numbers)	16 bytes	Any numeric value up to the range of a Double
    Variant
    (with characters)	22 bytes + string length	Same range as for variable-length String
    User-defined
    (using Type)	Number required by elements	The range of each element is the same as the range of its data type.
    Note   Arrays of any data type require 20 bytes of memory plus 4 bytes for each array dimension plus the number of bytes occupied by the data itself. The memory occupied by the data can be calculated by multiplying the number of data elements by the size of each element. For example, the data in a single-dimension array consisting of 4 Integer data elements of 2 bytes each occupies 8 bytes. The 8 bytes required for the data plus the 24 bytes of overhead brings the total memory requirement for the array to 32 bytes.
    
    A Variant containing an array requires 12 bytes more than the array alone.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    for data type sizes have a look in the vb5 help files
    there is a Data Type Summary
    Mark
    -------------------

  4. #4

    Thread Starter
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Thanks guys, but how about
    Boolean 2 bytes True or False
    A boolean has 2 possibilities 1 or 0, why can't you use 1 bit for it then? True (1) or False (0)?

    And how about the decimal point, how do you convert it to Binary? Thanx
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Let's see if I can quickly answer a couple of your
    questions.

    From your original post:
    But how does the computer know if it's a decimal or not?

    A computer knows nothing, it simply follows instructions.
    It is the compiler that creates the instructions in a code
    (a.k.a. machine language) that the computer can follow.
    The compiler creates a symbols table that contains the
    memory address (actually, it's an offset value), type and
    size for every variable. Based on the info in the symbols
    table, the compiler generates the machine language
    instructions to complete the operation. The loader
    converts the offset to an actual address when the program
    is launched.

    Each architecture (in this case, x86) has its own
    representation for real numbers. The only time I have
    worked with real numbers at this level was in a BAL class
    in college back in the 70’s. If you want to see how an IBM
    360 stored real numbers, I can probably find an example in
    one of my old texts.

    From your second post:
    1) A boolean has 2 possibilities 1 or 0, why can't you use
    1 bit for it then? True (1) or False (0)?

    A byte is the smallest addressable segment of memory.

    2) And how about the decimal point, how do you convert it
    to Binary?
    The decimal point is ‘understood’. Part of the variable’s
    memory space is allocated to the whole part of the number
    and the rest is allocated to the fractional part. These
    parts have names, but as I said, it’s been a while and I
    forget the names. Unless you are working in assembler, you
    just let the compiler 'worry' about the details.



  6. #6
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Talking Boolean

    You have to admit, they could have and should have made the boolean more efficent. I mean if you were gonna have 50 boolean variables, you would be using 100 bytes vs if you declared them as Byte you would only be using 50 bytes and then you could just use a value of 0 and 1 for true and false. It doesnt make much sense to me that they made it two bytes, but hey, they are Microsoft, what are you gonna do? hehe

    Just MHO,
    KillemAll

  7. #7
    Guest
    If my CS teacher is right...

    Decimal points are done in the same concept as integers, etc.. In unsigned integers, the bits count from 2^0 (1) to 2^15 (32768).

    In doubles, the first four bytes are for integers (positive powers of 2), and the last four bytes are for fractions (negative powers of 2).

    For example, 00000000|00000000|00000000|00000111|11100000|00000000|00000000|00000000 (which can be written as 111.111, where the "." indicates the middle of the bit string) is really just

    2^2 + 2^1 + 2^0 + . + 2^-1 + 2^-2 + 2^-3
    = 4 + 2 + 1 + 0 + .5 + .25 + .125
    = 5.875.

    In this form, 5.5 can be written as 101.1 in binary.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width