Bitwise complement operator

Hi,
I have an int x = 5;
0000 0000 0000 0000 0000 0000 0000 0101
~x
converts to:
1111 1111 1111 1111 1111 1111 1111 1010
Now, x = -6 WHY????
I know that the 32th digit represents the -ve sign. How to compute the number "6"?
Thanks for help.
gogo

0000 0000 0000 0000 0000 0000 0000 0000 => +1
0000 0000 0000 0000 0000 0000 0000 0000 => 0
1111 1111 1111 1111 1111 1111 1111 1111 => -1
1111 1111 1111 1111 1111 1111 1111 1110 => -2
1111 1111 1111 1111 1111 1111 1111 1101 => -3
1111 1111 1111 1111 1111 1111 1111 1100 => -4
1111 1111 1111 1111 1111 1111 1111 1011 => -5
1111 1111 1111 1111 1111 1111 1111 1010 => -6
Is this what you're wondering?

Similar Messages

  • How can i do bitwise (binary) operations (AND, OR, XOR)

    I need to do bitwise (binary) operations such as AND, OR, and XOR.  For example, how do I AND 10101000 with 11111000?

    for OR, with 10101000 in A1 and 11111000 in A2, try:
      =1×SUBSTITUTE(A1+A2,2,1)
    Result:  11111000
    and for XOR, try:
       =1×SUBSTITUTE(A1+A2,2,0)
    Result: 1010000  (missing leading 0)
    You can display missing leading zeros by setting the cell's Data Format to 'Numeral System' with Base set to 10 and Places to 8, or turn the result into a string with something like this (assuming the result is in A3):
         =RIGHT("0000000"&A3,8)
    SG

  • Bitwise XOR Operator

    I am going through a certification book, that I have found to have errors. I am not real familiar with bitwise operations, but I have come across one that I think is in error. Can someone confirm or deny whether the following is correct?
    00001010
    ^ 00001000
    00001000
    Thanks

    The problem that I posted here was one of many I have found in the certifcation book called "All In One Java 2 Exam Guide". I see on Amazon.com that there is new version of this book being offered in October.
    I would recommend that you avoid this book and its new version. There are errors in text. There are errors in code examples. There are errors in questions. There are errors in the answers to the questions. As many reviewers on Amazon.com noted, this is one of the worst books on the market. There was obviously a rush to market. I can't imagine anyone with any technical knowledge proofread the book.
    God Bless America

  • Bitwise And operator

    If the binaryStrings of two ints "a" and "b" are different lengths will the & operator always return zero? i.e
    int a =8;
    int b =4;
    String binarya = "1000"
    String binaryb = "100"
    System.out.println(8,4); --> 0
    Thanks

    847102 wrote:
    Kayaman wrote:
    There is no "different length", the two parameters will always have the same length. Bitwise AND will work on bytes, shorts, ints and longs, meaning 8-bit, 16-bit, 32-bit or 64-bit "lengths".
    Each resulting bit is 1 if both input bits are 1, otherwise the resulting bit is 0.The answer 8 & 4 = 0Both are ints. Both are 32 bits.
       00000000 00000000 00000000 00001000  // 8
       00000000 00000000 00000000 00000100  // 4
       00000000 00000000 00000000 00000000
    1000
    100That's the same as if, in 2nd grade arithmetic, when you're adding 1000 + 100, you do
       1000
       100
    +_____
       2000Edited by: jverd on Apr 20, 2011 8:35 AM

  • Bitwise shift operator...

    I know this is very basic but I've never done this...
    If I have an object that is an Integer, and I want to increase it by 1, isn't this how you do it?
    Object ob=new Integer(10);
    ob=ob>>1;it tells me that >> operator cannot be applied to object, int
    So what can it be applied to..?

    Ummmm,sadly, no... :(
    I really do lack basic computer skills, I don't know
    a lot about binary and stuff... but how would you do
    what I want to do..?I asked because it's quite a weird code. Just checking.
    OK, so:
    the binary shift operator can only be applied between to ints, that is primitives, no Objects (like Integer is).
    This operator is not used to increase a number, but to shift its bits by one to the right.
    Usage:
    int i,j;
    j = 10;
    i = j >> 1;
    Integer obj = new Integer(10);
    int res = obj.intValue() >> 1;
    Integer resObj = new Integer(res);

  • Subtraction of a positive integer of a negative integer results a positive?

    Hi
    I have the next problem and I haven't found the reason...
    I have the next Java code:
    int a = -2147483648;
    int b = 2147483647;
    System.out.println(a - b);
    The output of this lines is "1", but I don't understand why.
    Thanks

    >
    My main issue was that I wasn't sure of the Java process for this operation, and I thought that maybe the result
    -4294967295 = FFFFFFFF00000001
    could throw an exception.
    >
    The answer to that concern is in the Java Language Spec.
    http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf
    See sections 4.2.1 for the value ranges (you will see your MAX and MIN 'int' values there
    >
    4.2.1 Integral Types and Values
    The values of the integral types are integers in the following ranges:
    • For byte, from -128 to 127, inclusive
    • For short, from -32768 to 32767, inclusive
    • For int, from -2147483648 to 2147483647, inclusive
    • For long, from -9223372036854775808 to 9223372036854775807, inclusive
    • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
    4.2.2 Integer Operations
    The Java programming language provides a number of operators that act on integral
    values:
    • The comparison operators, which result in a value of type boolean:
    ◆ The numerical comparison operators <, <=, >, and >= (§15.20.1)
    ◆ The numerical equality operators == and != (§15.21.1)
    • The numerical operators, which result in a value of type int or long:
    ◆ The unary plus and minus operators + and - (§15.15.3, §15.15.4)
    ◆ The multiplicative operators *, /, and % (§15.17)
    ◆ The additive operators + and - (§15.18)
    ◆ The increment operator ++, both prefix (§15.15.1) and postfix (§15.14.2)
    ◆ The decrement operator --, both prefix (§15.15.2) and postfix (§15.14.3)
    ◆ The signed and unsigned shift operators <<, >>, and >>> (§15.19)
    ◆ The bitwise complement operator ~ (§15.15.5)
    ◆ The integer bitwise operators &, ^, and | (§15.22.1)
    • The conditional operator ? : (§15.25)
    • The cast operator (§15.16), which can convert from an integral value to a value
    of any specified numeric type
    • The string concatenation operator + (§15.18.1), which, when given a String
    operand and an integral operand, will convert the integral operand to a String
    representing its value in decimal form, and then produce a newly created String
    that is the concatenation of the two strings
    >
    These are the two statements that apply directly to your question and concern
    >
    Otherwise, the operation is carried out using 32-bit precision, and the result of the
    numerical operator is of type int. If either operand is not an int, it is first widened
    to type int by numeric promotion.
    The integer operators do not indicate overflow or underflow in any way.
    >
    Yours are 'integer operators' operating on 'int' values.

  • Logical bitwise operator

    Hi ALL
    Logical bitwise comparison operator 'O' is used between two operands say A and B as "A O B".
    A is of type INT1 and B is of type X.
    The above mentioned line was working fine till enabling unicode check. But now it says A should be of type X or Xstring.A's type cannot be changed since it is derived from a std structure.
    Is there some other operator which does the same function as operator 'O'?
    Regards,
    Navaneeth

    Hi,
    Check this..
    IF.....XOR.....
    Regards
    Vijay

  • SCJP prep help -- bitwise bad boys?

    Hi,
    I'm studying for SCJP so obviously one of the details I have to brush up on are the oft-unused bitwise operators: & | ^ ~
    I'm wondering if there are any mental techniques to using these in your head. Instead of literally writing out the number in binary, performing the comparison, and then re-converting back to decimal.
    Similar to how in grade school we learn that you can take a shortcut of multiplying two long numbers:
    34563
    x 24
    and do these steps:
    3 * 4 = 12, carry the 1
    6 * 4 = 24, add the 1 is 25, carry the 2
    5 * 4 = 20, add the 2 is 22, carry the 2
    4 * 4 = 16, add the 2 is 18, carry the 1
    3 * 4 = 12, add the 1, it all equals 138252
    next line
    then pad a zero on the right
    3 * 2 = 6
    6 * 2 = 12, carry the 1
    5 * 2 = 10, add the 1 is 11, carry the 1
    4 * 2 = 8, add the 1 is 9
    3 * 2 = 6, it all equals 691260
    altogether the answer is 691260 + 138252 = 829512
    In other words, while it's not feasible to add 34563 to itself 24 times (23 actually), there is a trick to figuring it out. Is there something like that for bitwise operators?
    Thanks.

    Thanks -- I finally collated some good tips a few days after I posted that here, with some help from local LUGgers. I'll post them here for the archives:
    Would you mind posting a summary of the helpful tips, tricks, and
    shortcuts sent your way? I'm rather interested in understanding this
    stuff a little better myself.Sure. In fact, they say you really know something well when you can
    explain it to someone else, and this gives anyone a chance to correct me
    if I'm wrong in what I think I know. Here's some of the things I
    learned, combined with what I had already learned from my book (please
    do correct me if I'm wrong):
    1. The unary bitwise NOT operator (~) has a shortcut, but one which
    only works when two's complement is in effect. On systems in which
    one's complement is in effect, it doesn't work. The shortcut is that
    any number n has a bitwise NOT of -n - 1. So
    ~5 == -6
    ~-12 == 11
    Because I'm fortunate enough to be working in Java where you're writing
    for only one platform (the JVM, which has a spec), I don't have to worry
    about exceptions to this for the test.
    2. Other than the NOT operator, the bitwise operators are binary
    operators, working on pairs of numbers. The bitwise AND operator
    returns a 1 for each pair of bits that are both 1.
    So in the case of
    11001 & 10011
    the result is 10001 (because only the first and last digits are both 1).
    The bitwise OR (|) operator returns a 1 for situations in which either
    pair of bits has a 1.
    So in the case of
    11001 | 10011
    the result is 11011 (because the only place in both numbers where there
    is no 1 is the third digit).
    The bitwise XOR (^) operator returns a 1 for situations in which either
    pair of bits, but not both, has a 1 (hence the name "exclusive OR", or XOR).
    So in the case of
    11001 ^ 10011
    the result is 01010 (because in the first, third, and fifth digits,
    either both numbers are 0 or 1).
    The short circuit operators AND (&&) and OR (||) work just like their
    regular counterparts except they stop evaluating once they "know" the
    result (AND stops evaluating if the first operand is false, and OR stops
    evaluating if the first operand is true).
    3. So my original question, "does anyone know any mental shortcuts for
    working with bitwise operators", should have been phrased as "does
    anyone know any mental shortcuts for converting decimal numbers to
    binary". The answer, which a couple of people provided, was that it's
    much easier to convert to binary (even if only in your head) when you
    are working from octal or hexadecimal, and that it's not too difficult
    to convert from decimal to octal or hexadecimal.
    Several ways were suggested:
    a) Convert the number to hex, then do the comparison a byte at a time in
    your head using a table:
    & 0x01 0x02 ... 0xff
    0x01 0x01 0x00 0x01
    0x02 0x00 0x02 0x02
    0xff 0x01 0x02 0xff
    b) Convert the number to octal using a simple recursive divide-by-eight
    solution:
    divide the number by eight, store the remainder as the low bit depth
    (rightmost number), then repeat the process with the quotient.
    123 / 8 = 15 remainder 3 -- 3 is rightmost digit (3)
    15 / 8 = 1 remainder 7 -- 7 is next to 3 (73)
    we don't divide 1 by 8 -- 1 is next to 7 (173)
    so 123 in octal is 0173
    From octal it's much easier to see the binary form of the number:
    1 7 3
    001 111 011
    And from binary it's much easier to do a bitwise calculation.
    c) Another way to convert to binary is to simply keep dividing by two
    and assigning 1 if there's a remainder and 0 if there isn't, then read
    in reverse to get the binary number:
    <quote>
    If a number is even, dividing it by 2 will have 0 remainder.
    If it is odd, dividing it by 2 will have a 1 remainder. Dividing
    by 2 is so easy that I, at least, can just write down a column of
    successive halvings, along with 1s and 0s for their oddness.
    459 1
    229 1
    114 0
    57 1
    28 0
    14 0
    7 1
    3 1
    1 1
    Reading from bottom to top, we again get 111001011. In this
    presentation you stop when the quotient is 1, since you've already
    written down the remainder that you'd get by dividing 1 by 2 when
    you write down the oddness. Remainder when dividing by 2 is easier
    than quotient.
    </quote>
    d) A final useful tip mentioned: as long as x is not zero,
    x & -x
    will always evaluate to a power of two, which means only one bit is set
    in the number (the least significant of the bits of x that were 1s).
    4. And everyone agreed that it's foolish to do these kinds of
    computations in your head when there's no good reason not to use a
    computer to do it. My personal preference is to use Python since the
    interactive interpreter works just like a calculator, though any
    scripting language should probably be able to handle the calculations.
    (Here's a quick sample:)
    |$ python
    |Python 2.2.2 (#1, Dec 31 2002, 12:24:34)
    |[GCC 3.2 20020927 (prerelease)] on cygwin
    |Type "help", "copyright", "credits" or "license" for more information.
    |>>> 12 & 14
    |12
    |>>> 12 | 14
    |14
    |>>> 12 ^ 14
    |2
    |>>> ~12
    |-13
    |>>> myResult = 0x5422CA66 & 458
    |>>> myResult
    |66
    |>>> hex(myResult)
    |'0x42'
    (hey, it wouldn't be a LUG without a little evangelism, would it? :)
    Much thanks to Bill, Jason, Kevin, and YATArchivist.
    Erik

  • Bitwise Operators .. stupid question ?

    I have a HashMap which contains Strings.. The String are actually numbers.
    I want to use Bitwise operators of and or, xor on them... How so I do it?
    I want to say result = string1 and string2...... I am not sure how stupid this sounds... but I am sure I am too close to it....

    Example answer to your original question, wanna:
    Hashtable<Object, String> ht = new Hashtable<Object, String>();
    ht.put("twenty eight", "28");
    ht.put("five", "5");
    ht.put("twenty", "20");
    System.out.println(Integer.valueOf(ht.get("twenty eight")) & Integer.valueOf(ht.get("five")) & Integer.valueOf(ht.get("twenty")));Which, of course, prints "4" to standard out. You're best off using a Hashtable<Object, Integer>, though.
    In answer to your second question,
    ~ is indeed the compliment operator. E.g:
    System.out.println(1 + ", " + (~1));
    prints "1, -2", as you might expect.

  • Bitwise logical ops on src/dest. bitmaps

    Those familar with the C/C++ world should understand what I
    mean by bitwise logical operations, i.e. performing logical AND,
    OR, NOT on the bits of a src and destination value to get a result.
    Those familiar with Win32 GDI and BitBlt know that it allows
    ROP codes which enable the bits of a source and destination bitmap
    to be combined using bitwise logicial operations as above. So for
    example, the ROP code SRCAND would combine a source and destination
    bitmap using a bitwise logical AND operator on the binary bits of
    the two bitmaps.
    I need to do the same thing if possible in either
    actionscript or flex. I know flex has high level blendmodes for
    combining src and destination such as BlendMode.OVERLAY
    BlendMode.DIFFERENCE, (but no BlendMode.AND, BlendMode.OR). There
    is actually a BlendMode.INVERT which would be the same as NOT (so
    why would AND and OR be left off). Actionscript has the BitmapData
    class with methods like CopyPixels, which is essentially the same
    as Win32 BitBlt, except without any sort of ROP bitwise logical ops
    apparently.
    With all the various type of complex masking you can do in
    actionscript, I find it hard to believe that no basic bitwise
    logical ops between source and destination bitmaps are possible. Is
    there a way to do them?
    Thanks.

    Did you read this?: https://wiki.archlinux.org/index.php/Sa … #Backlight

  • Bitwise functions in grapher?

    hi there. i want to graph a function that has a bitwise xor operator. the help was no help. how do i do it?
    for reference, here's the function i want to graph (in c notation, hence the ^ is bitwise xor instead of exponent):
    int x, y;
    y = x + 10000 + 200*(x^11025);

    Hi, have you parused the list of built in functions, I can see none that would suffice, yet did not investigate all the ones I didn't know what they meant.

  • Bitwise AND in sql?

    Hi gurus and gurettes,
    In different programming languages I've used bitwise AND operations to determine if a certain bit in a number is set. e.g.
    i := (129 and 130);
    print i;
    (answer would be 128 since only bit 128 is present in both numbers)
    10000001 (129)
    10000010 (130)
    -------- AND
    10000000 (128)
    My question: how do i do this in sql? It seems sql only knows AND for logical evaluations (true/false).
    Thanx,
    Lennert

    on Oracle 9 this works:
    SQL> select bitand(129,130) from dual;
    BITAND(129,130)
    128
    however, on oracle 815 it gives me this:
    SQL> select bitand(129,130) from dual;
    ERROR:
    ORA-00932: inconsistent datatypes
    I looked in the Oracle 8i documentation and I couldn't find 'bitand'. was this introduced in O9? is there an equivalent on O8i?
    Cheers,
    Lennert

  • Perform XOR operation in BPEL 10g

    How can we perform bitwise xor operation in transformations in BPEL 10g.
    As a part of requirement, i need to add xor operation.
    Can anyone help on this.
    Thanks
    Phani

    How can we perform bitwise xor operation in transformations in BPEL 10g.
    As a part of requirement, i need to add xor operation.
    Can anyone help on this.
    Thanks
    Phani

  • Java Bit arithmatic support?

    Does java provide support for bitwise operation as does C++ and many other languages?
    It seems tht as a fully OO language, it should implement some very powerfull classes for bit manipulation and such. If you could point me in the right direction for learning the support, it would be great.
    ~Andy

    Besides the BitSet class that was already provided in another answer, java also has the bitwise operators:
    Bitwise and: &
    Bitwise or: |
    Bitwise complement: ~
    Bitwise xor: ^
    Bitwise left shift: <<
    Bitwise signed right shift: >>
    Bitwise unsigned right shift: >>>
    tajenkins

  • Help with code...Logic is kinda messed up...

    Modify the file copy program so that the copy is encrypted (encoded). Each byte of the source file is altered by reversing each bit. For example,
    input byte
    output byte
    00110101
    11001010
    00000000
    11111111
    10000000
    01111111
    This operation is sometimes called a bit-wise complement. "Bit-wise" means that each bit is treated independently of all the others. "Complement" is just another word for reversal.
    All the bits in an integer can be reversed as follows:
    int value;
    value = ~value;
    The "~" (tilde) is the bit-wise complement operator. Visually it looks like a reversal of up and down. It reverses all the bits in value, even though you may be interested in only the low-order byte. But since the operation is bit-wise, the result for the low-order byte is the same no matter how many others are affected.
    And here is my logic..I kno its a mess...so excuse me.
    int tempo;
    try
    source = new DataInputStream (new BufferedInputStream(new FileInputStream( filename )));
    catch ( IOException a )
    Errors.setText (String.valueOf("Problem opening " + filename) );
    try
    dest = new PrintWriter( new BufferedWriter(new FileWriter( filename2 ) ) );
    catch ( IOException b )
    Errors.setText (String.valueOf("Problem opening " + filename2) );
    try
    try
    while ( true )
    tempo += source.readInt();
         tempo += ~tempo;
    dest.println(tempo);
    catch ( EOFException c )
    source.close();
    catch (IOException d )
         System.out.println( "IO Problems with" + filename2 );
    I need to modify this logic so that it encrypts the file through the damned bitwise thingy...TIA.

    This code:
    tempo += source.readInt();
    tempo += ~tempo;
    dest.println(tempo);Needs to change to this:
    tempo = source.readInt();
    dest.println(~tempo);Or,
    dest.println(~ source.readInt());

Maybe you are looking for

  • CSS not being accessed on certain Web sites

    When I access www.thebytestopshere.com (my Web site), none of the text is rendered according to the rules set forth in the .css file. It does render correctly if I access the files from my C:\ drive. Everything renders properly in Internet Explorer,

  • Authentication mode in OAAM

    Hi, I am new with OAAM. In one of the models executed at pre-auth time I see a rule checking if the authentication mode of user is "Full Keypad". My question is: where I can change/read the authentication mode of a user? Directly in the the "VCRYPT_U

  • Safari 7.0.4 – White Flashing

    Hi, I am currently designing a website using Wordpress that uses animated GIFs in various sections of the site. I'm finding that on scrolling through a page using Safari that some white flashing is occurring. This white flash is very brief, only occu

  • Can anyone help me find the raw converter for Nikon D4/ I have Aperture 3.0 with Snow Leopard. thanks in advance

    hi, Having problems finding the correct download for a Nikon D4 raw converter. Can anyone help direct me to the right link? I am using Aperture 3.0 with Snow leopard. thanks in advance Mark

  • Reducing header height

    Hi, I want to edit header-height of CRM 6.0 Web UI.  I edited following properties in "thtmlb_stand.css" file: - th-l-logo         background color: #0066A1;         background position: right 7px;        /* height:             72px; */        height