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

  • Home
  • SEARCH
  • 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 5968271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:03:58+00:00 2026-05-22T20:03:58+00:00

This is the most weird thing that has ever happened to me when programming

  • 0

This is the most weird thing that has ever happened to me when programming in C++.

This is my main file:

#include <iostream>
#include "lib/utils.h"

using namespace std;

int main(int argc, const char *argv[]) {
    cout << bin2dec(101000010);
    return 0;
}

and this is lib/utils.cpp:

#include <iostream>
#include "utils.h"

int bin2dec(int bin) {
    // 101000010
    int dec;
    //std::cout << "";  // If you uncomment this, it works.
    for (int i = 1; bin > 0; i *= 2, bin /= 10) {
        if (bin % 2 == 1) {
            dec += i;
        }
    }
    return dec;
}

The program compiles with no warnings, and when run, it outputs 450. 450 is not 101000010 in decimal, 322 is. The first weird thing is that the difference between 482 and 322 is exactly 128. This happens with any binary number you try to convert. But what’s REALLY weird, is that when I tried to output the value of bin and dec inside the for in an attempt to debug the function, it suddenly started working correctly.

Basically, for some reason, if you std::cout something before the function returns, it works. If you don’t, it adds 128 to the result.

I’m using g++ 4.6.0, and compiling like this:

g++ -c   -D NDEBUG -O2     -o 10.o 10.cpp
g++ -c   -D NDEBUG -O2     -o lib/utils.o lib/utils.cpp
g++  -o 10   -Wl,-S  10.o lib/utils.o lib/menu.o 
  • 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-22T20:03:59+00:00Added an answer on May 22, 2026 at 8:03 pm

    You’re not initializing dec.

    int dec = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably one of the most common tasks / problems when programming; You
This most be the second most simple rollover effect, still I don't find any
I know I can do most of this by hacking Trac and using Git
This is probably the most classic database problem. I have an E-commerce software solution
This is one of the most used Regex functions Regex.IsMatch(Test text for regex test.,
this c# code is probably not the most efficient but gets what I want
Compared to most people on this site I am admittedly a novice. I wanted
Note: This is the opposite direction to most similar questions! I have an iPhone
I think most people know how to do this via the GUI (right click
I have a UIView container that has two UIImageView s inside it, one partially

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.