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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:12:22+00:00 2026-06-08T10:12:22+00:00

I have tried to make a longer function but it has acted very weird.

  • 0

I have tried to make a longer function but it has acted very weird. I had tried to isolate the problem and I have been able to find the buggy part.

This program made for an Arduino but this phenomenon probably appear in other environment. I have tried to make a lot of searches but I cannot find the solution.

So, my buggy part:

Why do not these two codes give the same result?
How can I construct a one line function without extra variable
but same operation like “Code 1”?

Results:

  • Code 1: 0b00101100
  • Code 2: 0b01101100

Source codes:


Code 1: (correct operation but not one line)

#include <binary.h>

const byte value=B00110110;
byte buffer,result;

void setup(){
  Serial.begin(115200);

  buffer = (value << 3);
  result = (buffer >> 2);

  Serial.println(result,BIN);
}

void loop(){
}

It gives: 0b00101100


Code 2: (incorrect operation but one line)

#include <binary.h>

const byte value=B00110110;
byte result;

void setup(){
  Serial.begin(115200);

  result = ((value << 3) >> 2);

  Serial.println(result,BIN);
}

void loop(){
}

It gives: 0b01101100


  • 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-06-08T10:12:22+00:00Added an answer on June 8, 2026 at 10:12 am

    I assume that your byte is some unsigned integral type smaller than int.

    Most arithmetic operations in C++ are performed in the domain of int type, after all “smaller” operands are implicitly converted (promoted) to type int.

    For this reason, your first group of shifts is equivalent to

    buffer = (byte) ((int) value << 3);
    result = (byte) ((int) buffer >> 2);
    

    while your second group if shifts is equivalent to

    result = (byte) (((int) value << 3) >> 2);
    

    In the first variant, any bits that get shifted outside the range of type byte by the << 3 shift are lost during intermediate conversion and assignment to buffer. In the second variant all calculations are performed within the range of int, so nothing is lost.

    If you want your second variant to produce the same results as the first, you need to explicitly “truncate” the intermediate shift result

    result = ((byte) (value << 3) >> 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried searching over the internet about this problem but not able to
I tried to make admin panel and I am using sessions , but have
This problem has been bugging me for a while. I have to load a
I have tried to make a regex I use with php evolve, but by
I have tried to make the fullscreen feature of a SDI application with splitter
This might get a little confusing as I have tried everything to make this
How to make a sticky footer i have tried on google found some results
How do I make the following url forbidden in apache; main/index.php?site=ing I have tried
I've successfully been able to make elements (like div's, panel's, whatever) draggable by using
I have been trying to apply a style to my TabWidget. I've tried several

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.