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 761587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:51:21+00:00 2026-05-14T15:51:21+00:00

Why does the following raise a compile time error: ‘Cannot implicitly convert type ‘int’

  • 0

Why does the following raise a compile time error: ‘Cannot implicitly convert type ‘int’ to ‘byte’:

byte a = 25;
byte b = 60;

byte c = a ^ b;

This would make sense if I were using an arithmentic operator because the result of a + b could be larger than can be stored in a single byte.

However applying this to the XOR operator is pointless. XOR here it a bitwise operation that can never overflow a byte.

using a cast around both operands works:

byte c = (byte)(a ^ b);
  • 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-14T15:51:22+00:00Added an answer on May 14, 2026 at 3:51 pm

    I can’t give you the rationale, but I can tell why the compiler has that behavior from the stand point of the rules the compiler has to follow (which might not really be what you’re interesting in knowing).

    From an old copy of the C# spec (I should probably download a newer version), emphasis added:

    14.2.6.2 Binary numeric promotions This clause is informative.

    Binary numeric promotion occurs for
    the operands of the predefined +, ?,
    *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary
    numeric promotion implicitly converts
    both operands to a common type which,
    in case of the non-relational
    operators, also becomes the result
    type of the operation. Binary numeric
    promotion consists of applying the
    following rules, in the order they
    appear here:

    • If either operand is of type decimal, the other operand is
      converted to type decimal, or a
      compile-time error occurs if the other
      operand is of type float or double.
    • Otherwise, if either operand is of type double, the other operand is
      converted to type double.
    • Otherwise, if either operand is of type float, the other operand is
      converted to type float.
    • Otherwise, if either operand is of type ulong, the other operand is
      converted to type ulong, or a
      compile-time error occurs if the other
      operand is of type sbyte, short, int,
      or long.
    • Otherwise, if either operand is of type long, the other operand is
      converted to type long.
    • Otherwise, if either operand is of type uint and the other operand is of
      type sbyte, short, or int, both
      operands are converted to type long.
    • Otherwise, if either operand is of type uint, the other operand is
      converted to type uint.
    • Otherwise, both operands are converted to type int.

    So, basically operands smaller than an int will be converted to int for these operators (and the result will be an int for the non-relational ops).

    I said that I couldn’t give you a rationale; however, I will make a guess at one – I think that the designers of C# wanted to make sure that operations that might lose information if narrowed would need to have that narrowing operation made explicit by the programmer in the form of a cast. For example:

    byte a = 200;
    byte b = 100;
    
    byte c = a + b;  // value would be truncated
    

    While this kind of truncation wouldn’t happen when performing an xor operation between two byte operands, I think that the language designers probably didn’t want to have a more complex set of rules where some operations would need explicit casts and other not.


    Just a small note: the above quote is ‘informational’ not ‘normative’, but it covers all the cases in an easy to read form. Strictly speaking (in a normative sense), the reason the ^ operator behaves this way is because the closest overload for that operator when dealing with byte operands is (from 14.10.1 “Integer logical operators”):

    int operator ^(int x, int y); 
    

    Therefore, as the informative text explains, the operands are promoted to int and an int result is produced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT a.id, a.aname, COALESCE(b.bname, '') FROM a LEFT JOIN b… May 14, 2026 at 8:00 pm
  • Editorial Team
    Editorial Team added an answer You want an unselectable select? <%= Html.ListBoxFor( m => m.Choices,… May 14, 2026 at 8:00 pm
  • Editorial Team
    Editorial Team added an answer After several experiments and help from various other groups, I… May 14, 2026 at 8:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.