开发者

Sum of the bits in Java

sorry Friends i did a mistake. I have did this mistake again. am really very sorry.

this is the Issue.

I have a time range like

int Starttime = 2 which mean(02:00)
int enttime =  8 which mean(08:00)

i want time in sum of bits, example

     00:00 1  
     01:00 2    
     02:00 4  -----
     03:00 8   R
     04:00 16  a
     05:00 32  n
     06:00 64  g
     07:00 128 e
     08:00 256 -----

and soo on till 23:00

so i need totalRange = 256+128+64+32+16+8+4 ;

it should开发者_JAVA百科 be like this sorry again.

Thanks


The table indicates that you want to map the hour value of a time to an integer value using this function:

int value = (int) Math.pow(2, hourValue);

or, in other words, 00:00h will map to 20, 12:00h to 212 and so on.

Now if you have need the sum of start and endtime, you can simple use the function from above and add the values:

int totalrange = (int) Math.pow(2, starttime) + (int) Math.pow(2, endtime);

Now if you have starttime=2 and endtime=23, this will give a result (written in binary):

01000000 00000000 00000100

shamelessly adapting polygenelubricants much faster solution:

int totalrange = (1 << starttime) + (1 << endtime);

This works, because 2i is equal to (1 << i).


    System.out.println(Integer.toBinaryString(2+24)); // 11010

This uses the Integer.toBinaryString method. There's also toHexString, toOctalString, and a toString with variable radix.

If you need the string to be zero-padded to a specific width, you can write something simple like this:

static String binaryPadded(int n, int width) {
    String s = Integer.toBinaryString(n);
    return "00000000000000000000000000000000"
                .substring(0, width - s.length()) + s;
}

//...
System.out.println(binaryPadded(2+24, 8)); // 00011010      

There are different ways to zero pad a string to a fixed width, but this will work for any int value.

For hexadecimal or octal, you can use formatting string instead:

    System.out.println(String.format("%04X", 255)); // 00FF

The specification isn't very clear, but it looks like you want this mapping:

0 -> 1
1 -> 2
2 -> 4
3 -> 8
4 -> 16
:
i -> 2i

In that case, your mapping is from i to (1 << i) (the << is the bitwise left-shift operator).

    System.out.println(
        (1 << 2) + (1 << 4)
    ); // 20

Note that depending on what is it that you're trying to do, you may also consider using a java.util.BitSet instead.


BitSet demonstration

This may be completely off-the-mark, but assuming that you're doing some sort of interval arithmetics, then BitSet may be the data structure for you (see also on ideone.com):

import java.util.BitSet;
//...

static String interval(BitSet bs) {
    int i = bs.nextSetBit(0);
    int j = bs.nextClearBit(i);
    return String.format("%02d:00-%02d:00", i, j);
}

public static void main(String[] args) {
    BitSet workTime = new BitSet();
    workTime.set(9, 17);
    System.out.println(interval(workTime));
    // 09:00-17:00

    BitSet stackOverflowTime = new BitSet();
    stackOverflowTime.set(10, 20);
    System.out.println(interval(stackOverflowTime));
    // 10:00-20:00

    BitSet busyTime = new BitSet();
    busyTime.or(workTime);
    busyTime.or(stackOverflowTime);
    System.out.println(interval(busyTime));
    // 09:00-20:00
}

Note that methods like nextSetBit and nextClearBit makes it easy to find empty/occupied time slots. You can also do intersect, or, and, etc.

This simple example only finds the first interval, but you can make this more sophisticated and do various arithmetics on non-contiguous time intervals.


Integer.toBinaryString(i) 

Returns a string representation of the integer argument as anunsigned integer in base 2.


To compute the length of the time-interval, you have to do

int totalrange = endtime - starttime;

Perhaps this is what you're looking for:

int startTime = 2;
int endTime = 24;
int range = endTime - startTime;

System.out.println(range + " can be expressed as the following sum:");
for (int size = 1; range > 0; size <<= 1, range >>= 1)
    if ((range & 1) != 0)
        System.out.format("+ %02d:00%n", size);

Output:

22 can be expressed as the following sum:
+ 02:00
+ 04:00
+ 16:00
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜