|
-
Oct 5th, 2000, 11:23 AM
#1
Thread Starter
Lively Member
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-*
-
Oct 5th, 2000, 12:08 PM
#2
Addicted Member
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
-
Oct 5th, 2000, 12:15 PM
#3
Guru
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!
-
Oct 5th, 2000, 12:21 PM
#4
New Member
Well, Im not sure about this but could it be that one of the 16 bits are reserved for the sign?
-
Oct 5th, 2000, 12:25 PM
#5
Monday Morning Lunatic
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
-
Oct 5th, 2000, 02:32 PM
#6
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|