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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:13:35+00:00 2026-06-07T00:13:35+00:00

I am interested in doing this C code in Java: // sets n’s ith

  • 0

I am interested in doing this C code in Java:

// sets n's ith bit from right, i starts from 0
void setBit(int* n, int i){
   *n = *n | (1 << i);
}

However, it looks like java can’t pass addresses, so what would be some clean approaches?

I thought of two approaches, but I was wondering if there are better ways to do it?

Approach 1: using an array

 // sets n[0]'s ith bit from right, i starts from 0
public void setBit(int[] n, int i){
   n[0] = n[0] | (1 << i);
}

Approach 2: using a class

private class Data{
   int value;
}

// sets d.value's ith bit from right, i starts from 0
public void setBit(Data d, int i){
   d.value = d.value | (1 << i);
}
  • 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-07T00:13:36+00:00Added an answer on June 7, 2026 at 12:13 am

    Nope, no better way to do it…

    Unless you’d like to do it the traditional Java way, which is

    d = setBit(d, i);
    
    public int setBit(int d, int i) {
      return d | (1 << i);
    }
    

    This is all to say in Java that “modifying arguments to a function” is almost inherently unclean. The clean way is to find some alternative to modifying the arguments.

    (Sometimes it’ll be inevitable, in which case your workarounds are the way to go. That said, if you want to write a method modify(myData), it’s frequently better to add a modify() method to myData‘s class.)

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

Sidebar

Related Questions

Disclaimers: I am not interested in doing this in any real production code. I
I'm interested in doing some Java web development but I'm not really interested in
Interested in upgrading JAVA VERSION from JAVA 1.5 to JAVA 1.6 [oracle@server301 /]$ java
I would like to embed Clojure code into Java. This site was helpful in
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
Update I've updated this question to include source code of a Java implementation, using
I'm interested in doing some development against the Safari browser but I'm not sure
I'm interested in doing something like an enum but where the increments aren't always
I'm a .NET web developer, who's bought a Mac, and is interested in doing
I am interested in knowing about what other teams are doing about limiting internal

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.