2
Not getting the char value: 
#define XL 33 
#define OR 113 
#define NOR 313 
#define TN 344 

int to_bits(int critn,char *mask) 
{ 
unsigned int x; 
int begin; 

if (critn < XL) begin = 1; 
else if (critn < OR) begin = XL; 
else if (critn < NOR) begin = OR; 
else if (critn <= TN) begin = NOR; 
else begin = 0; 
x = critn - begin; 

*mask = (char)(0x80 >> (x % 8)); 

return (int)(x >> 3); // fast divide by 8 
}

For explanations and example of the method please see Explain the following C++ method.

I have written the following method in Java:

ClassA{

final static int xl = 33;
final static int or = 113;
final static int nor = 313;
final static int tn = 344;

public int to_bits(int critn){
    int x;
    int begin;

    if(critn<xl)
     begin = 1;
    else if (critn<or) 
     begin = xl;
    else if (critn<nor)
     begin = or;
    else if (critn<tn) 
     begin = nor;
    else begin = 0;

     x = critn - begin;

    char mask = (char)(0x80 >> (x % 8));
    System.out.println(mask);

    return (int)(x >> 3);

}


public static void main(String args[]){
    ClassA a =new ClassA();
    a.to_bits(312);

}

}

Can we take the output of mask into Bitset object. I am getting out put as (a special char output for the char field)

24.

Please suggest also how to get both the character and integer values. thanks

Community
  • 1
  • 1
JavaBits
  • 2,005
  • 11
  • 36
  • 40

3 Answers3

2

Are you asking if you can output the mask as binary?

If so, this will do the job:

    System.out.println(Integer.toBinaryString(mask));

or in hexadecimal:

    System.out.println(Integer.toHexString(mask));

(EDIT - I previously said to use Integer.toString(mask, 2 /* or 16 */), but that formats the number as signed ... which is obviously confusing you.)


You will eventually also want to know to return more than one value from a java method call. The answer is to return the values in an array (or object); e.g.

    return new int[]{(char)(0x80 >> (x % 8)), x >> 3});

or set them in a mutable array or object passed as a parameter.


I have a feeling that you have not realized that char is a 16 bit type in Java. If you want a type that is equivalent to a C / C++ char, the closest is the Java byte type - a signed 8 bit type.


On further reflection, the simplest approach is to do the calculations to give you int values; e.g.

    return new int[]{0x80 >> (x % 8), x >> 3});

and have the caller cast either value to some other type if required.

Assuming that these values are intended to be used for bitwise operation, the fact that they might look negative is irrelevant ... provided that the bit patterns are correct. Remember that they are bit patterns and not numbers. If that is confusing, use Integer.toHexString(...) or Integer.toBinaryString(...) to print them out in unsigned form.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks a lot . Yes but as it is of type signed its returning negative sign(-) before the value. – JavaBits Apr 08 '11 at 14:00
  • If it is really a bit mask, the fact that there is a negative sign is irrelevant. You are not using it as a number: you are using it as a bit pattern. – Stephen C Apr 09 '11 at 01:08
0

This can be useful if not considering the sign. Else we can stick to the char.

//char mask = (char)(0x80 >> (x % 8)); byte mask = (byte)(0x80 >> (x % 8)); System.out.println(Integer.toString(mask,2));

output: 1 24

JavaBits
  • 2,005
  • 11
  • 36
  • 40
0

The short answer is: There is no way to return more than one value.

The long answer is: You can put more than one value into one value. If you want to return only a few values and all your values are of the same type, use an array. (see Stephen C's answer) Otherwise use an Object. For your case:

class MaskAndBits {
      char mask;
      int bits;
      public BitsAndMask(char mask, int bits) {
        this.mask = mask;
        this.bits = bits;
      }
 }

and then return new Whatever((char)(0x80 >> (x % 8));, (int)(x >> 3)). Overall this is a lot more to type than the array solution, but its harder to mix up the two values returned ;-)

subsub
  • 1,857
  • 10
  • 21