Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:35:43+00:00 2026-05-25T20:35:43+00:00

I have the following code: short myShort = 23948; byte myByte = (byte)myShort; Now

  • 0

I have the following code:

short myShort = 23948;
byte myByte = (byte)myShort;

Now I wasn’t expecting myByte to contain the value 23948. I would have guessed that it would contain 255 (I believe the largest value for a byte).

However, it contains 140, and it made me wonder why; what is actually going on behind the scenes?

Please note that I am not looking for someone to solve the problem that 23948 cannot fit into a byte, I am merely wondering about the underlying implementation

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:35:43+00:00Added an answer on May 25, 2026 at 8:35 pm

    Short is a 2-byte type and a byte is, well, a single byte. When you cast from two bytes to one you’re forcing the system to make things fit and one of the original bytes (the most significant) gets dropped and data is lost. What is left from the value of 23948 (binary: 0101 1101 1000 1100) is 140 which in binary translates to 1000 1100. So you are going from:

    0101 1101 1000 1100 (2 byte decimal value 23948)
    

    to:

              1000 1100 (1 byte decimal value 140)
    

    You can only do this with an explicit cast. If you tried assigning a short to a byte without a cast the compiler would throw an error because of the potential for loss of data:

    Cannot implicitly convert type ‘short’ to ‘byte’. An explicit
    conversion exists (are you missing a cast?)

    If you cast from a byte to a short on the other hand you could do it implicitly since no data would be getting lost.

    using System;
    public class MyClass
    {
        public static void Main()
        {
            short myShort = 23948;
            byte myByte = (byte)myShort; // ok
            myByte = myShort; // error: 
    
            Console.WriteLine("Short: " + myShort);
            Console.WriteLine("Byte:  " + myByte);
    
            myShort = myByte; // ok
    
            Console.WriteLine("Short: " + myShort);
        }
    }
    

    With arithmetic overflow and unchecked context:

    using System;
    public class MyClass {
        public static void Main() {
            unchecked {
                short myShort = 23948;
                byte myByte = (byte)myShort; // ok
                myByte = myShort; // still an error
                int x = 2147483647 * 2; // ok since unchecked
            }   
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: new Thread(new ThreadStart(delegate() { // something short })).Start(); Can
I have the following code snippet. public static void main(String[] args) { short a
I have the following code: struct A { short b; }; struct B {
I have the following XML code. <firstname> <default length=6>Örwin</default> <short>Örwin</short> <shorter>Örwin</shorter> <shortest>�.</shortest> </firstname> Why
Long story short Say I have the following code: // a class like this
If i have some code like the following: short myShortA = 54; short myShortB
I have the following code fragment: NSString* value = @value; NSString* key = @key;
I have the following code: boolean Short = x(); boolean Long = y(); boolean
I have the following code, which basically takes values from a database and populates
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.