-7

I got this code somewhere from the internet

final int time = 80 << 3 + 1;

  • 5
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Matt Ball Aug 29 '13 at 17:18
  • 1
    `<<` is the bit shift left operator. `24 << 8` means take the bits in the number 24, and shift them 8 places to the left. – Robert Harvey Aug 29 '13 at 17:19
  • Wow Matt Ball i was literally about to post the same link – progrenhard Aug 29 '13 at 17:21
  • tasksBegin[MINUTE_UNIT_NUM - 1] += tasksBegin[0]; means take the integer at index (MINUTE_UNIT_NUM - 1) of taskBegin, add the first element of taskBegin to it and put it back into the index (MINUTE_UNIT_NUM-1) in the array. – Grammin Aug 29 '13 at 17:21
  • 1
    @Flashamo Googling it would have saved many "seconds" of 41 people here, and few minutes of yours ! – abhishek14d Aug 29 '13 at 17:22
  • @Flashmo 1. [When to use Bitwise Operators ...](http://stackoverflow.com/q/261062/767881), 2. [Bitwise and Bit Shift Operators](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) – Ravinder Reddy Aug 29 '13 at 17:34

3 Answers3

2

<< is a left shift operator

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.

So 24 << 8 means shift binary value of 24 towards left by 8 bits position.

Follow reference to learn more about it.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

It means bit-shift the constant integer value 24 left by 8 bits.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
0

24 << 8 means shifting the number 24 to the left for 8 bits, which is equivalent to 24 * (2^8) = 6144.

In the provided code snippet, it encodes a time hh:mm into an integer hh << 8 + mm. Since there are 24 hours in one day, the array to represent the activity of every minute in one day requires (24 << 8) + 1 elements. The +1 is to make the array index for 24:00 legal.

timrau
  • 22,578
  • 4
  • 51
  • 64
  • The real question is why you would ever put a literal expression in code. You can always precompute them. Maybe in the case of bitshifting it makes the number look less arbitrary. Do you know if literal expressions are computed at compile time or run time? – Cruncher Aug 29 '13 at 17:24
  • Sir can you tell me why is it being used in the code. That would be very helpful – Flashmo Flash Aug 29 '13 at 17:27
  • @FlashmoFlash Please at least provide the usage of the code snippet. Otherwise people can hardly know what it is for, what input it expects, or how it works, not to mention "why `(24 << 8) + 1`." – timrau Aug 29 '13 at 17:29
  • We have to calculate the maximum number of overlapped “intervals” at the same time . I have added the interval class. – Flashmo Flash Aug 29 '13 at 17:45
  • @FlashmoFlash The intention is to mix the hour and the minute into one single integer. It uses the lowest 8 bits to store the _minute_ part, and the higher 5 bits for the _hour_ part. – timrau Aug 29 '13 at 17:47
  • Sir i would be very much happy if you could explain the same in some more detail. – Flashmo Flash Aug 29 '13 at 17:52
  • @FlashmoFlash Edited answer to explain more. You should figure out how it works by yourself since it's quite straightforward. The only trick was _how to encode a time in date into an integer for array indexing_. Think about how people do the job by _pencil and paper_. – timrau Aug 29 '13 at 18:00