Results 1 to 6 of 6

Thread: Bits, Bytes and all things nice....

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    68

    Unhappy

    Hi

    I have been doing vb for a while now, and I am a little confused over Integer's.

    Ok, if you set all the bits to one in a 16 bit integer to 1 the total is 32768, so why does an integer overflow if you try to put anything above 32767 into it????? Where have I lost the last bit????
    THE DOUGSTER!!!!!!


    *-MCSD-*

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    I think the reason is as follows. But I am not sure of it.

    As a matter of fact, a 16-bit binary "integer" can hold a number ranging from 0 to 65535. But according to VB, the range of an integer is -32767 to 32767. That is to say, in VB only 15 bits (which can hold a number ranging from 0 to 32767) of an integer are used to hold numbers (1 or 0) and the first bit is used as a "flag" to indicate whether the number is negative or not. That is why we get an overflow error when we put a number larger than 32767 into an integer.

    Am I right?

    ---------------------------
    Visual Basic Professional 6.0

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    That's right, except: The range is -32768 to 32767.

    A 15-bit integer can be used to express 32768 different numbers.
    For signed 16-bit integers (this is what the VB type Integer uses), if the negative flag is 0, then the 15 remaining bits express the 32768 numbers between 0 and 32767.
    If the negative flag is 1, then the 15 remaining bits express the 32768 numbers between -32768 and -1.

    You are right!

  4. #4
    New Member
    Join Date
    Jun 1999
    Location
    India
    Posts
    7

    Question

    Well, Im not sure about this but could it be that one of the 16 bits are reserved for the sign?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Hehe...good idea to read the other posts first...

    I think that's already been said .

    This is one of the problems with using VB for Win32 API stuff, because some things require an unsigned integer, which has the 16th bit usable as part of the number.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Yes, this is a stupid VB problem.
    Why do we need 4 bytes (Long) to express a number >32767 ?
    C or assembler are different and are my favorites when it comes to bit manipulation routines. Bit 15 can always be filled in or set to zero. Only when calculating with the values, one should pay attention to the sign bit!!!
    It's sad, that, when dealing with thousands of records with values (possibly) >32767, we have to use a long in VB and an integer in the DB field. VB is mega, but sometimes it really sucks.

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