You are not logged in. Please login at www.codechef.com to post your questions!

×

binary operation

int x= 6 & -6 ; then x= 2 how does the bitwise operation take place ? ANd how in -6 represented in binary form ?

asked 19 Dec '12, 22:55

mohit2104's gravatar image

4★mohit2104
1112
accept rate: 0%


May be the link above already helped you . But to focus exactly on your question I am posting an answer . Six is represented as 110 in base 2 . That's simple right . It can changed back into decimal form by : 0 * 2^0 + 1 * 2^1 + 1 * 2^2 . Also note that there are preceding or leading zeroes in the binary representation of 6 . So exact representation is 000......000110 For representation of a negative number , just represent the number as if the negative sign is not there . Then subtract one from it . Remember you don't need a representation of negative 0 , as negative 0 and positive zero is the same thing . So subtracting 1 is always possible . After subtraction take the bitwise complement of the number . Now you have the representation of the negative number . So for -6 we have 0000.....00110 ( ignoring the sign ) Then subtract one to get , 0000.....00101 Then take bitwise complement to get , 1111...111010

The bitwise operations happens exactly as you would expect them , just doing the operation A op B for the i'th bit in A and i'th bit in B and storing the result in i'th bit in result .

link

answered 20 Dec '12, 11:56

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

Perhaps this could help. Take a look at the second answer: it'll make things clear.

link

answered 19 Dec '12, 23:45

jigsaw004's gravatar image

3★jigsaw004
7627
accept rate: 0%

int(16 bits)

6 --> 0000000000000110

-6 --> 1111111111111010 (2's complement of(6))

(0000000000000110) AND(&)(1111111111111010)

ans is 2 --> 0000000000000010

link

answered 20 Dec '12, 21:55

p_onkar's gravatar image

0★p_onkar
11
accept rate: 0%

edited 20 Dec '12, 22:00

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×184
×1

question asked: 19 Dec '12, 22:55

question was seen: 1,195 times

last updated: 20 Dec '12, 22:00