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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:35:38+00:00 2026-05-29T19:35:38+00:00

Why do shift operations on unsigned ints give an unsigned result, but operations on

  • 0

Why do shift operations on unsigned ints give an unsigned result, but operations on smaller unsigned operands result in a signed int?

int signedInt = 1;
int shiftedSignedInt = signedInt << 2;

uint unsignedInt = 1;
uint shiftedUnsignedInt = unsignedInt << 2;     //OK. unsigned result

short signedShort = 1;
int shiftedsignedShort = signedShort << 2;

ushort unsignedShort = 1;
uint shiftedUnsignedShort = unsignedShort << 2; //CS0266: Can't cast int to uint

sbyte signedByte = 1;
int shiftedSignedByte = signedByte << 2;

byte unsignedByte = 1;
uint shiftedUnsignedByte = unsignedByte << 2;   //CS0266: Can't cast int to uint
  • 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-29T19:35:40+00:00Added an answer on May 29, 2026 at 7:35 pm

    The shift operators are predefined only for these cases (shift left):

    int operator <<(int x, int count);  (1)
    uint operator <<(uint x, int count); (2)
    long operator <<(long x, int count);  (3)
    ulong operator <<(ulong x, int count); (4)
    

    The expression uint shiftedUnsignedShort = unsignedShort << 2 is interpreted as (1)-st case (implicit up-casting from ushort to int and (int)2), so it performed a warning on illegal casting (there is no implicit cast from int result to ushort).
    The same situation we can see for uint shiftedUnsignedByte = unsignedByte << 2. It also interpreted as (1)-st case (implicit up-casting from byte to int and (int)2 ,but no implicit cast of resulting value to uint).

    You can resolve these issues using the following approach:

    uint shiftedUnsignedShort = (uint)unsignedShort << 2  //force use the (2)-nd shift operator case  
    uint shiftedUnsignedByte = (uint)unsignedByte << 2;   //force use the (2)-nd shift operator case
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an unsigned int, I have to implement the following operations : Count the
I want to shift away from Excel to Awk. I need basic mathematical operations,
I need to bitwise shift a value 64 times in JavaScript. But JavaScript starts
On left shift of (char) 0xff by 8 and casting it to int we
#include <iostream> #include <fstream> #include <cstring> using namespace std; typedef unsigned long int WORD;
I want to compare two multiplication methods implemented in Java which use shift operations
this code is supposed to calculate : p/4+v/8 using shift operations Data Segment Db
In 3 operations how can I turn a byte into a 32 bit int
I'm trying to find a way to perform an indirect shift-left/right operation without actually
In C, are the shift operators ( << , >> ) arithmetic or logical?

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.