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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:14:28+00:00 2026-06-18T16:14:28+00:00

Here’s a function I wrote in C++ (which works) and am now trying to

  • 0

Here’s a function I wrote in C++ (which works) and am now trying to convert to Java:

static String parseAInstruction(String line) {
    int n = getValue(line);
    if (n>=0) {
        String inst = "0";
        for (int i=14; i>=0; --i) {
            if (Math.pow(2, i) & n)
                inst += "1";
            else
                inst += "0";
        }
        return inst;
    } else {
        return "error";
    }
}

This compiles just fine in C++, but when trying to compile in Java, I get the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from int to boolean

    at parser.Parser.parseAInstruction(Parser.java:38)
    at parser.Parser.main(Parser.java:50)

Looking up bitwise AND in Java, it seems that it should be the same. It shouldn’t have anything to do with booleans at all. Can anyone tell me the problem?

  • 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-18T16:14:30+00:00Added an answer on June 18, 2026 at 4:14 pm

    In Java you can only use boolean values in if clauses. So the result of a bitwise AND of two numbers is not valid in an if statement.

    Try this instead: if ((Math.pow(2, i) & n) != 0) {.

    [Edit] However, as commenter @jlordo points out, next you’ll have to resolve the problem of doing a bitwise AND between a double and an integer.

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is my situation: I am making a countdown app which works fine but
Here is what I am trying to achieve in PHP: I have this string:
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Here is my code <?php function random_id() { $chars = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $id = '';
Here are two ways of setting context: $.proxy(function() { this.doStuff(); }, this); ... var
Here is a bitmap, which has a strange header. How was it created? What
Here's the code #include <iostream> #include <cmath> int main() { float c, d; for(int

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.