8 Replies - 758 Views - Last Post: 17 April 2019 - 12:57 PM Rate Topic: -----

#1 Alcatraz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 19

when and why might you chose to use a Byte instead of an Integer?

Posted 14 April 2019 - 09:25 PM

Please give me an example of this I'm trying to learn when and why you might chose to use a Byte instead of an Integer.

I cannot think of a way this would be used? I understand;
A Byte = 8 bits or 1 Byte Range -128 to 127
An Integer = 32 bits or 4 Bytes Range - 2,147,83,648 to 2,147,83,647

An integer is a whole number can be declared
When I need to change from a signed Byte to an unsigned variable, this helps with space and Data bandwidth.

BUT my example in code is poor

(Public class
public main(string)
byte byteVariable = 135)

This example produces an error and won’t be able to convert from Int to Byte/ its been assumed by vb that it is an Integer but It wont fit into the byte variable.

Is This A Good Question/Topic? 0
  • +

Replies To: when and why might you chose to use a Byte instead of an Integer?

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,044
  • Joined: 12-December 12

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 15 April 2019 - 12:20 AM

These days, in most cases, we could just use an integer as described here.

You could use byte if you find it makes your intent clearer, and the range of values will never be exceeded. I am sure there are plenty of examples of such a small range, although, two that occur off the top of my head are indices for a board game, which has 64 squares, or a range of basic colours.

These days, we are not concerned with saving storage, and the integer value is optimized for use as described at the provided link. It can be used, however, if it clarifies the intent of our code.

Also, if the value will be used with bitwise operators (bit twiddling).

byte byteVariable = 135 This is outside the range of values for a Byte.
Was This Post Helpful? 1
  • +
  • -

#3 Gooppoiner   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 12-April 19

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 15 April 2019 - 03:15 AM

I've used Byte (note, not SByte) in quite a few occasions, like with RGB values, with encoding/decoding, password and stream processing, serialization, etc... those are just at the top of my head.

Anyway, I don't get what you guys are talking about, Byte is from 0 to 255, so value of 135 should be valid.

Dim value As Byte = 135


What would be out of range is the following:

Dim value As SByte = 135


Now as mentioned before, I really don't remember the time when I had to use SByte, it's not common for me.
Was This Post Helpful? 2
  • +
  • -

#4 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,044
  • Joined: 12-December 12

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 15 April 2019 - 03:26 AM

Sorry, you are correct, the default is unsigned for Byte, so 0 to 255, and the assignment of the value 135 should not cause an error.
Was This Post Helpful? 1
  • +
  • -

#5 Alcatraz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 19

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 15 April 2019 - 03:16 PM

Thank you for your feedback.

I'm trying to find out why you would use a Byte instead of an integer.

Would you use a Byte to hold the integer amount because the integer amount is too large?

It may not be common practice but this is a bit of research and I cannot find the information on google. Or Microsoft vb tutorials/articles.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,044
  • Joined: 12-December 12

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 15 April 2019 - 11:54 PM

Haven't we already explained and provided a link?

Quote

Would you use a Byte to hold the integer amount because the integer amount is too large?

The opposite. You might use a byte because you know that the range of values needed will fit within a byte and, more importantly, you consider that this makes the intention of your code clearer. Or it will be used for bit arithmetic.

In the early days of personal computers we would use the smallest amount of storage necessary, because memory space was limited. Nowadays we tend to use integer for any reasonable integral value for the reasons described in the link I provided.

But I find that I am repeating myself...
Was This Post Helpful? 0
  • +
  • -

#7 Alcatraz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 19

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 17 April 2019 - 02:36 AM

Well thank you for explaining again. I find your second explanation clearer.

I have just joined this forum...getting used to the layout so maybe you dont know that..are all the words with the blue under your response links?

Yes my value range was wrong.

But I've been asked to research in what instance would you use a byte instead of an integer. And I was under the impression they were like russian dolls that if a value was too large it fitted or could be converted into another data type to save space and computer memory. That is why an integer could be converted to a byte of up to 4 bytes.

slow to learn.

thank you for answering you don't need to again.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6791
  • View blog
  • Posts: 28,044
  • Joined: 12-December 12

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 17 April 2019 - 02:52 AM

Ok.

Quote

I have just joined this forum...getting used to the layout so maybe you dont know that..are all the words with the blue under your response links?

Yes.
Was This Post Helpful? 1
  • +
  • -

#9 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: when and why might you chose to use a Byte instead of an Integer?

Posted 17 April 2019 - 12:57 PM

In most modern day cases you would not really need to be concerned with using a Byte type over a 4 byte Integer data type unless you are working with specific methods which require you to use them. There are many cases where you would use them as Gooppoiner has mentioned. Using a Byte instead of an Integer was a more popular way to think when computer RAM and hard drives where much smaller than any modern day machines, and every Byte was very precious. 8)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1