aspose file tools*
The moose likes Java in General and the fly likes C Macro to Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "C Macro to Java" Watch "C Macro to Java" New topic
Author

C Macro to Java

Mike Phillips
Greenhorn

Joined: Sep 07, 2012
Posts: 3
I need to convert the following C Macro to Java. Any suggestions would be appreciated.

#define set_bit(array, bit) ((array)[(bit) / 32] |= (1L << ((bit) % 32)))

array is an unsigned long and bit is an int

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

well..do you know what the macro does?

The first step is to figure that out. tell us, in english, what it is doing. Once you can do that, writing it in Java (or any other language) is much easier.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mike Phillips
Greenhorn

Joined: Sep 07, 2012
Posts: 3
fred,

Thanks for your reply.

The best I can get from looking at it (Macro) is that it is manipulating the unsigned long array using bitwise operators.

for example the unsigned long starts out as a Zero value (i.e. 0000000000000000 ) then the macro is passed in a different long array value ..... such as 0000000000000001 or 0000000000008000 along with it's corresponding 'reference number' that could be a number between 1-60). The long values are added together to make the final value 0000000000008001.

I have actually gotten this to work but by using an entirely different design approach, however, it took me several weeks to come up with the way this works. Basically I created a new class composed with Enum values with each possible value that can used such as...

public Enum enumValues
{
private static long VALUE_1 0000000000000001;
private static long VALUE_2 0000000000000002;
private static long VALUE_3 0000000000000004;
private static long VALUE_4 0000000000000008;
private static long VALUE_5 0000000000001000;
private static long VALUE_6 0000000000002000;
private static long VALUE_7 0000000000004000;
private static long VALUE_8 0000000000008000;
....
public String getNewValue(long currentValue, long refNumber)
{
return calculateValue(currentValue, refNumber);
}
private String calculateValue(long currentValue, long newValue)
{
// add NewValue to currentValue using packed decimal notation.

return newValue;
}
}

I then add the current value with any new values using packed decimal addition to get the final value.

This does work, however, I am wondering if there is a cleaner/more efficient approach to doing this.




William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

You should not have to declare an Enum for every single value.
Java has just the same functionality.

I wish I had my old Turbo-C compile handy ;)

WP
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Welcome to the Ranch, Mike!

Mike Phillips wrote:The best I can get from looking at it (Macro) is that it is manipulating the unsigned long array using bitwise operators.

This description is pretty vague. There are probably infinitely many ways to program bitwise manipulation of an array of ints in Java.

I'm not understanding the rest of your description, but I'm afraid it is a bit off. For starters, there is only one array in the expression. An item of this array is accessed using an index computed from the bit variable. And so on.

Actually, Java happens to support this kind of operation out of the box. You don't need to write a single method (let alone an enum). But before I'll tell you what it is, I'd like you to try to understand the logic on your own once more.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Mike Phillips wrote:
#define set_bit(array, bit)
array is an unsigned long and bit is an int


A riddle as a hint.

If you did not have the macro "body", only its name and the type its arguments, that is, only the information above, what would your guess be, what it does?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Mike Phillips wrote:I need to convert the following C Macro to Java. Any suggestions would be appreciated.

I guess my question would be: what does the person who's asking you to "convert" this mean by the word 'convert'?

Do they simply want you to understand what the macro is doing, or is the implementation supposed to "look" the same as the original?

Java has no equivalent of header files or macros, so the only way to make it "look" the same is to make it a method; and then you'll have to decide what it's scope should be. Should it be static? Should it be globally available?

Quite a few decisions to think about apart from just the implementation.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
jQuery in Action, 2nd edition
 
subject: C Macro to Java