This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# C Macro to Java

Mike Phillips
Greenhorn
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
Posts: 12098
30
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.

Mike Phillips
Greenhorn
Posts: 3
fred,

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
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
Posts: 3752
62
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
Posts: 972
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
Posts: 10243
58
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