Results 1 to 7 of 7

Thread: Carrot sign

  1. #1

    Thread Starter
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Carrot sign

    public class Class {

    public static void main(String[] args)
    {
    for(int i = 1; i <= 100; i++)
    {
    System.out.print((i^2) + ",");
    }
    }
    }

    so i got that code, what does the carrot sign do in java, cause that sure as hell doesn't print out the squares of i...
    My monkey wearing the fedora points and laughs at you.

  2. #2

    Thread Starter
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423
    ok, i sorta figured out what ^ does, it performs what is called bitwise exclusive or, i read some stuff on what that does and was confused...
    My monkey wearing the fedora points and laughs at you.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It manipulates the bits. You see, all numbers in computers are in binary. The number 177, for example, is in binary
    10110001
    The bitwise operators <<, >>, >>>, ^, &, | and ~ manipulate numbers of the bit level. ~ simply inverts all bits:
    ~10110001 = 01001110
    <<, >> and >>> shift the bits:
    10110001 << 3 = 10001000
    >> and >>> shift to the right instead of left. They differ in the way new bits get inserted on the left, but I forgot which is which.
    &, ^ and | perform bitwise logical operations: & is AND, ^ is XOR and | is OR.
    10110001 &
    01100101 =
    00100001
    so only where both operands have 1 has the result 1. OR means where either have 1:
    10110001 |
    01100101 =
    11110101
    and XOR where either, but not both (exclusive or):
    10110001 ^
    01100101 =
    11010100
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, for simple powers like square you usually write it in multiplications: x^2 = x*x.

    For more complicated powers, you use the Math.pow function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    For the ~ operator you can do some simple binary manipulation to turn neg nums to positive and visa versa.

    neg to pos (-n == ~n +1)

    -484 = 10 0001 1100
    01 1110 0011 = 483
    483 + 1 = 484

    pos to neg (n == ~n +1)
    484 = 01 1110 0100
    10 0001 1011 = - 485 + 1 = - 484

    same formula applies.....

    pos to neg ((n-1) ~n == (-n))
    484 - 1 = 483
    483 = 01 1110 0011
    -484 = 10 0001 1100

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, but - does it too
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width