3 Replies - 484 Views - Last Post: 01 September 2010 - 08:10 AM Rate Topic: -----

#1 irv_the_swerve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 09

Code definitions

Posted 01 September 2010 - 07:03 AM

Hi all,

Would anybody be able to explain what the following code parts mean (this is part of a wider program but I understand the rest of it)

theArray[charPos] &= ~(1L << i);



I'm just wondering what '1L' means (I have also seen '~0L' too...something to do with the 'long' type? Does the tilde mean 'approx equal to' as in mathematical notation?).

Secondly I'm wondering what '<<' means...(the program utilises a bit-shift function if that helps).

Searched google and Java sources but can't find anything...any help would be great! Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: Code definitions

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Code definitions

Posted 01 September 2010 - 07:20 AM

Well the '~' is a bitwise operator is actually the logic operator NOT:
from sun's tutorial:

Quote

The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

'L' indicates a long constant.
in your code, the number '1' could be applied to an int variable as well. to indicate that it is a long variable, you use L, like: '1L'.

The signed left shift operator "<<" shifts a bit pattern to the left.
in your code, it shifts 1L i tims to the left.
for example, suppose the value is:
00000001 (represents 1 in 8 bits).
if you apply the << operator, like so:
1 << 1 (move one bit to the left) it would be:
00000010.

remember, that '<<' does not update the value in the number itself.
if you have something like this:
int a = 1;
a << 2;

This code Wont change the value of a.
if you want to keep the value of a, you will have to assign it:
a = a << 2;


anyways...
I'd really recommend taking a look at a bits tutorial:
http://www.javabegin...rators-tutorial
Was This Post Helpful? 3
  • +
  • -

#3 irv_the_swerve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 09

Re: Code definitions

Posted 01 September 2010 - 07:29 AM

Thanks very much for your reply :) I understand now...(it's my first time working with bit-operations lol). And thank you for the tutorial website, I've been looking for something like that :)
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Code definitions

Posted 01 September 2010 - 08:10 AM

No problem :)
Glad I could help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1