Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Size? Boolean vs Byte

Size? Boolean vs Byte

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studioquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    watagal
    wrote on last edited by
    #1

    Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005

    D J 2 Replies Last reply
    0
    • W watagal

      Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      A boolean in the .NET Framework is not just a single bit, but is a structure with a couple fields and a bunch of methods. Internally, the boolean value is stored at an Int32, or 32-bit value, I think(!). RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      • W watagal

        Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005

        J Offline
        J Offline
        jo0ls
        wrote on last edited by
        #3

        The system memory bus is 4 bytes (32 bits). The minimum packet of information along this bus then is 4 bytes. The common language runtime decides the actual size of the boolean at runtime. Typically it will use 4 bytes as this is the most efficient. If memory is at a premium, then it can reduce the size on the fly. On 64 bit architecture it will use 8 bytes.

        W 1 Reply Last reply
        0
        • J jo0ls

          The system memory bus is 4 bytes (32 bits). The minimum packet of information along this bus then is 4 bytes. The common language runtime decides the actual size of the boolean at runtime. Typically it will use 4 bytes as this is the most efficient. If memory is at a premium, then it can reduce the size on the fly. On 64 bit architecture it will use 8 bytes.

          W Offline
          W Offline
          watagal
          wrote on last edited by
          #4

          Thanks to all. My goal is to learn the right way to design tight classes. Soooo on today's 32 bit system, using less than 4 Bytes is inefficient. This is beginning to make sense -- I also read using a 'int16' or 'short integer' (2 Bytes) is less efficient than using an 'int32' or 'integer' (4 Bytes). I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Thanks, Karen Nooobie to OOP and VB.Net 2005

          D 1 Reply Last reply
          0
          • W watagal

            Thanks to all. My goal is to learn the right way to design tight classes. Soooo on today's 32 bit system, using less than 4 Bytes is inefficient. This is beginning to make sense -- I also read using a 'int16' or 'short integer' (2 Bytes) is less efficient than using an 'int32' or 'integer' (4 Bytes). I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Thanks, Karen Nooobie to OOP and VB.Net 2005

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            watagal wrote: I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Not necessarily. Depends on what math your doing and what the result is going to end up in. But, you also don't ahve to be THAT tight with it. The size of the variable isn't really a concern because of the VAST memories of today's systems. What makes a "tight" class is the use of proper OOP techniques, data types, logic, class organization and relationships to other classes. At least IMHO. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups