Calculating in C# with the data type "byte"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lovro Mirnik
    New Member
    • Feb 2011
    • 8

    Calculating in C# with the data type "byte"

    Greetings,

    I need some help.
    In order to save memory space, I would like to perform calculations with the byte data type, instead of int.

    The number will never reach beyond 255, and will never be negative...
    The problem is described in the code, so please take a look.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ByteCalculation
    {
        class Program
        {
            static void Main(string[] args)
            {
                byte Number_1 = 10; // Declaring the value
                byte Number_2 = 12; // Declaring the value
                Console.WriteLine(Number_1 + Number_2); // Output is 22 - CORRECT!
                // ---------------------------------------------------------------
                // Here it's where it stops.
                // If I wanted to make a basic operation, such as add 1
                // I would have to declare 1 as a byte value.
                // ???????????????????????????????
                Number_1 = Number_1 + 1; // ?????? While, when having an int, this is possible.
                // ???????????????????????????????
                Console.ReadKey();
                // Could there be another solution, which wouldn't require me to declare each value?
            }
        }
    }
    If you perhaps have no time to test this out yourself, this is the Error List that Microsoft Visual C# 2010 Express displays:
    Code:
    Error	1	Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
    Any help will be greatly appreciated...

    Lovro Mirnik
    Attached Files
  • Lovro Mirnik
    New Member
    • Feb 2011
    • 8

    #2
    Solved

    Hello,

    I'm here to let you know that I've found a solution.
    A full topic covered on bytes can be found here.

    Thank you,
    Lovro Mirnik

    Comment

    Working...