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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:00:11+00:00 2026-06-13T01:00:11+00:00

Let’s say you’re writing a Java (or subset-of-Java) compiler and you want to generate

  • 0

Let’s say you’re writing a Java (or subset-of-Java) compiler and you want to generate bytecode for a unary not expression, !E. You’re past type checking so you know E has type boolean, i.e. it will push a 1 or a 0 on to the operand stack.

One way to do it is something like (in Jasmin syntax):

E
ifeq truelabel
iconst_0
goto stoplabel
truelabel:
iconst_1
stoplabel:

i.e. if there’s a 0 on the stack push 1, else push 0. Another way to do it, taking advantage of the fact that a boolean is just an int with value 1 or 0, is to say !E = (E + 1) % 2 and generate

E
iconst_1
iadd
iconst_2
irem

Is there an advantage to using one over the other? Or something else entirely?

  • 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-13T01:00:12+00:00Added an answer on June 13, 2026 at 1:00 am

    I wouldn’t count on the following definition to hold true on the bytecode level.

    true == 1
    

    On the binary level (and its almost language independent), a boolean is usually defined as

    false == 0
    true != 0
    

    The javac compiler apparently also follows this definition (all checks in javac bytecode I have seen just always checks agains ZERO, never against ONE).

    And it makes sense to use this definition for boolean instead only treating 1 as true, C also defines it this way (true is just != 0, not simply 1) and in assembly code this convention is also commonly used. So java also using this definition makes it possible to take/pass java booleans to other code without any special conversions.

    I suspect your first code example (with the ifeq) is the only way to correctly implement the not-operator for booleans. The ^1-method (xor with 1) will fail if the boolean value is not strcitly represented as 0/1. Any other int value would cause the expression to work incorrectly.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have thousands of users and I want to make the passwords
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's say I want to write an FTP client from scratch. In the command
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the

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.