Correct byte usage
Recently in a high-school IPT exam, we were supposed to declare a few variables, based on the scenerios.
For one segment, I was to declare a variable to hold data for the number of seats that a car has.
I used the 'Byte' data type.
This was marked wrong by the teacher, and when I asked him about it, he seemed to be rather unsure about the Byte data type altogether. Firstly, he said that it wouldn't be able to carry the number of seats that a car has. I pulled up the VB5 help file in front of him, showing where it said 'whole numbers 0-255'.
He muttered something about it not being the 'real-world programming standards' and then walked away, without giving me the question as correct.
I always assumed that, for efficiency, you want to use the smallest data type available to reserve memory.
Should I have used Integer instead?
Thanks,
Duncan
Hi Duncan,
I just lost a post explaining the reasons why you were right to use a byte. Think it's a case of real world programming habits rather than standards, it's just something developers (inc me) do. If you ever read code examples you'll see int's instead of bytes all the time. So the short version, you were right to use bytes (no reason to use ints) and you should continue to think about the data types you use, your teachers ego was bruised. Some software guys can get a bit egotistical when faced with their own lack of knowledge. These are generally the people who fake it until they make it, and eventually you'll get a job fixing all their code.
Your teacher is right, although it is hard to proof and the logic is dated. The CPU in your computer is most efficient when it deals with its "native" data type. For a 32-bit CPU, that's an Integer (System.Int32). On modern CPUs this is no longer relevant. Given a choice between a Byte, a Short and an Integer, most of your future colleagues would be a bit mystified why you chose a Byte. If you have VB.NET (the Express version is a free download), you need to try to compile this:
Option Strict On
Module Module1
Sub Main()
Dim b As Byte = 0
b = b + 1
End Sub
End Module
If you can't figure out why this doesn't compile, ask your teacher.
Hi Duncan, Derek & Hans,
By the way I mostly agree with Derek especially in the case of databases it is best to think of the field sizes.
I agree on the comments by Hans ( user i.d. nobugz ) that the INTEGER type is the most often used for loops however etcetera.
Discuss this code then which does compile with OPTION STRICT ON
The For next loop will run and it appears the body runs 255 times despite the fact the loop is 0 to 254 !!
( I have sussed this, 0 to 254 is 255 separate numbers as
Dim something(10) As Integer ' is 11 separate entities. )
NEXT obviously adds 1 each time and the last value of the loop is compared.
Once 254 is exceeded the loop terminates at the NEXT statement.
However the aByte variable reaches 255.
Change the loop to run from 0 to 255 and you get an overflow on the NEXT
statement as the program trys to add 1 to the value of 255 for a BYTE value ( it appears ).
Comment out the bright green highlighted line and change the FOR NEXT loop to go from 0 to 255 to see what I mean.
Code Snippet
Option Strict On
Public Class Form1
'The next highlighted line of code should be one line of code in your code window.>>>>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aByte As Byte = Convert.ToByte(0)
Dim msg As String = ""
For
index As
Byte
= 0 To
254If
index Mod
25 = 0 Then
msg &= index.ToString & ControlChars.NewLine
Else
msg &= index.ToString &
" "
End
If
'The loop body to runs 255 times.
aByte = aByte + Convert.ToByte(1)
'Next adds 1 and compares the last value of the FOR NEXT loop,
'if greater than the last value the FOR NEXT loop terminates.
Next
MessageBox.Show(msg)
MessageBox.Show(aByte.ToString)
End
Sub
End Class
Regards,
John.
P.S. If I was going to be pedantic, I would say the OP is correct.
A BYTE is the smallest numeric TYPE and it is still possible to do a loop with them too. 
nobugz wrote: |
Your teacher is right, although it is hard to proof and the logic is dated. The CPU in your computer is most efficient when it deals with its "native" data type. For a 32-bit CPU, that's an Integer (System.Int32). On modern CPUs this is no longer relevant. Given a choice between a Byte, a Short and an Integer, most of your future colleagues would be a bit mystified why you chose a Byte. If you have VB.NET (the Express version is a free download), you need to try to compile this: Option Strict On Module Module1 Sub Main() Dim b As Byte = 0 b = b + 1 End Sub End Module If you can't figure out why this doesn't compile, ask your teacher.
| |
Hi Hans,
It has been a little while since I played with SQL Server ( any version ) but I believe you can still store BYTE values?
If you want an optimised database then why not use the size of a BYTE for a field value?
Databases would be a reason to use BYTE values I guess.
This will compile though,
( the reason being is the 1 is treated as an Integer by the IDE, hover your mouse over it. )>>
Code Snippet
Option Strict On
Module Module1
Sub Main()
Dim b As Byte = 0
b = b + Convert.ToByte(1)
End Sub
End Module
Even if you put the module immediately after a FORM in the same code window like this.>>
Code Snippet
Option Strict On
Public Class Form1
'The next highlighted line of code should be one line of code in your code window.>>>>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aByte As Byte = Convert.ToByte(0)
Dim msg As String = ""
For
index As
Byte
= 0 To
254If
index Mod
25 = 0 Then
msg &= index.ToString & ControlChars.NewLine
Else
msg &= index.ToString &
" "
End
If
'The loop body appears to run 255 times!!
aByte = aByte + Convert.ToByte(1)
'Next adds 1 and compares the last value of the FOR NEXT loop,
'if greater than the last value the FOR NEXT loop terminates.
Call Main() 'Adds one to the Public variable "b" in the module.
Next
MessageBox.Show(msg)
MessageBox.Show("b = " & b.ToString)
MessageBox.Show("aByte = " & aByte.ToString)
End Sub
End Class
Module Module1
Public b As Byte = 0
Sub Main()
b = b + Convert.ToByte(1)
End Sub
End Module
Regards,
John.
Hi All,
If the ide was really horrible we would have to type this sort of thing all the time.
Reason: Hover your mouse over the numbers and it will tell you they are INTEGERs of course.
Code Snippet
For
index As
Byte
= Convert.ToByte(0) To
Convert.ToByte(254) Next
Code Snippet
Module Module1
Public b As Byte = Convert.ToByte(0)
Sub Main()
b = b + Convert.ToByte(1)
End Sub
End Module
Regards,
John.
John, Nobugz, Duncan, how you doing guys,
Oh I don't know about this, I think the byte would have been the correct (or better) choice here and judging by what Duncan wrote of his teachers reaction so did he. Bytes aren't good for mathematic operations (as John has demonstrated quite well) but in this case, the number of car seats, then your very unlikely to do any heavy mathematics with it, it will very rarely change, and will most commonly be < 5 (depending on the car). So I think the byte suits the nature of the data nicely here.
However nobugz is champeen developer and his advice is invaluable but his post begs the question, what is the point of the byte then, you know, why is it there? It must have it's place and this is an example I think, as well as for database as Johns pointed out, of that place.
Couple of things worth pointing out though, end of the day its not terribly important really with the amount of memory that modern computers have whether an integer or a byte was used, what is more important is it wasn't wrong to use a byte, it would have done the job. Duncans teacher should have recognised this as well as Duncans ability to demonstrate, through looking it up in the help files, that it would do the job and instead of going in a big girly strop he should have swallowed his pride and marked Duncans answer correct.
The forum only has input from one side.
The Basic language didn't have a Byte type for many, many years. I'm pretty sure no version up to some Visual Basic release. But to do the low-level stuff, close the operating system, having Byte is pretty essential. It is the basic unit of storage. You can't twiddle bitmap bits or convert a Socket stream or translate a string from one codepage to another without it. Microsoft wanted to hand the VB programmer the ability to do this as well as the next 'C' programmer. Thus Byte got added. Emphasizing here that Byte is useful as a unit of storage. As a data type to do arithmetic with, it sux rox. Can't express negative values, overflows in a hurry. Even as a type that saves memory it is useless. Structure aligning makes it occupy 4 bytes anyway, might as well use an Integer. Only as an array does it make sense. These are the kind of details that the OP's teacher may well have elected to walk away from. Wise move.
Ok ok, I concede. Tell you what though the lad Duncan did well, he thought there was a problem, he enquired about it, he demonstrated that perhaps the byte would do, must of hit a bit of a brick wall because he then came here and asked about it. Thats pretty good. Still think his teacher never helped him out like he should of, the lad wouldn't have came here if he did.