{
    "componentChunkName": "component---src-templates-learn-js",
    "path": "/learn/ds/bit_manipulation",
    "result": {"data":{"mdx":{"frontmatter":{"title":"Bit Manipulation","date":"July 24, 2021","tags":["ds"],"description":"Bit Manipulation"},"body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Bit Manipulation\",\n  \"date\": \"2021-07-24\",\n  \"description\": \"Bit Manipulation\",\n  \"tags\": [\"ds\"],\n  \"type\": \"tuts\"\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar Callouts = makeShortcode(\"Callouts\");\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Bit Manipulation is the process of applying logical operation on a sequence of bits to achieve a required result.\\nTasks that require bit Manipulation include low-level device control, encryption algorithms and optimization.\"), mdx(\"p\", null, \"Bit Manipulation makes use of the \", mdx(\"strong\", null, \"Bitwise\"), \" operation.\"), mdx(\"p\", null, \"AND \", \"[ & ]\", \" , OR \", \"[ | ]\", \" , XOR \", \"[ ^ ]\", \", NOT \", \"[~]\", \", Bit Shifts \", \"[ << , >> ]\"), mdx(\"p\", null, \"Every number is stored in a binary format that is 0 and 1 .Bitwise operators are used for performing manipulation of bits of the number.\\nThese can be used with any type of Integer data types.\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"byte (8 bits )\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"short ( 16 bits )\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"int ( 32 bits )\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"long (64 bits)\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"char (16 bits)\")), mdx(\"h2\", null, \"1. Unary Bitwise Complement ( ~ ):\"), mdx(\"p\", null, \"Basically means bit negation .It takes every single bit of the number and flips its value i.e., ~0 becomes 1 & vice versa. It is the 1's Complement of the number\"), mdx(\"p\", null, \"a = 5 = 0101 (In binary)\"), mdx(\"p\", null, \"~ 0101 = 1010\"), mdx(\"p\", null, \"1010 = 10 (In Decimal)\"), mdx(\"h2\", null, \"2. Bitwise AND (&) :\"), mdx(\"p\", null, \"Unlike Bitwise Complement operator .other Bitwise operators need two operands.\"), mdx(\"p\", null, \"Bitwise AND is similar to logical AND in a sense that it results in 1 only when the two compared bits are both equal to 1 . otherwise, it results in 0.\"), mdx(\"p\", null, \"a = 5 = 0101 (In binary)\"), mdx(\"p\", null, \"b = 7 = 0111 (In binary)\"), mdx(\"p\", null, \"Bitwise AND operation of 5 and 7\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-css\",\n    \"metastring\": \"file=index.css show=false\",\n    \"file\": \"index.css\",\n    \"show\": \"false\"\n  }, \"// Both 1 means 1 ,else all 0\\n\\n0  1  0  1\\n0  1  1  1  &\\n------------\\n0  1  0  1   = 5 (In Decimal)\\n\")), mdx(Callouts, {\n    name: \"success \",\n    mdxType: \"Callouts\"\n  }, \"a & -1 = a (a is a imaginary number)\"), mdx(\"h2\", null, \"3. Bitwise OR ( | ) :\"), mdx(\"p\", null, \"OR results in 1 when atleast one of the compared bits is 1 (or both ) otherwise it results in 0\"), mdx(\"p\", null, \"a = 5 = 0101 (In binary)\"), mdx(\"p\", null, \"b = 7 = 0111 (In binary)\"), mdx(\"p\", null, \"Bitwise OR operation of 5 and 7\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-css\",\n    \"metastring\": \"file=index.css show=false\",\n    \"file\": \"index.css\",\n    \"show\": \"false\"\n  }, \"// In compared bits atleast or both 1 then 1, else 0\\n\\n0  1  0  1\\n0  1  1  1  |\\n------------\\n0  1  1  1   = 7 (In Decimal)\\n\")), mdx(Callouts, {\n    name: \"success \",\n    mdxType: \"Callouts\"\n  }, \"a | 0 = a (a is a imaginary number)\"), mdx(\"h2\", null, \"4. Bitwise Exclusive OR ( XOR ) ( ^ ):\"), mdx(\"p\", null, \"Xclusive OR (XOR ) results in 1 only if both compared bits have a different value, otherwise it results in 0.\"), mdx(\"p\", null, \"a = 5 = 0101 (In binary)\"), mdx(\"p\", null, \"b = 7 = 0111 (In binary)\"), mdx(\"p\", null, \"Bitwise XOR operation of 5 and 7\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-css\",\n    \"metastring\": \"file=index.css show=false\",\n    \"file\": \"index.css\",\n    \"show\": \"false\"\n  }, \"// In compared bits have diffferent then 1 , else 0\\n\\n0  1  0  1\\n0  1  1  1  ^\\n------------\\n0  0  1  0   = 2 (In Decimal)\\n\")), mdx(Callouts, {\n    name: \"success \",\n    mdxType: \"Callouts\"\n  }, mdx(\"table\", null, mdx(\"tr\", null, mdx(\"td\", null, \"a ^ 1 = \\u0101 = ~a \")), mdx(\"tr\", null, mdx(\"td\", null, \"a ^ 0 = a \")), mdx(\"tr\", null, mdx(\"td\", null, \"a ^ a = 0 \"))), \"(a is a imaginary number)\"), mdx(\"h2\", null, \"5. Signed Left Shift ( << ) :\"), mdx(\"p\", null, \"Signed Left Shift takes the bit pattern of the first operand and shifts it to the left by the number of places\\ngiven by the second operand\"), mdx(\"p\", null, \"Ex: 5 << 3 :\"), mdx(\"strong\", null, \"Every bit in the binary representation of the Interger 5 is shifted by 3 positions to the left.\"), mdx(\"p\", null, \"00000101 becomes 00101000\"), mdx(\"p\", null, \"5 << 3 is 40 .\"), mdx(\"p\", null, \"That shows that shifting a number by one is equivalent to multiplying it by 2 or more.\\nIn general , left shifting a number by n positions is equivalent to manipulation by 2 ^ n.\"), mdx(\"h2\", null, \"6. Signed Right Shift ( >> ):\"), mdx(\"p\", null, \"Signed Right Shift takes the bit pattern of the first operand and shifts it to the right by the number of places\\ngiven by the second operand\"), mdx(\"p\", null, \"However it preserves the sign ,Positive numbers remain positive and negative ones remain negative.similar to left shift,\\nthe right shift of n positions is equivalent to division by 2 ^ n or division by 2 ^ n-1 in case of add numbers.\"), mdx(\"p\", null, \"Ex: a =10 :\"), mdx(\"p\", null, \"a >> 1 = 5\"), mdx(\"p\", null, \"Ex : a= -10\"), mdx(\"p\", null, \"a >> 1 = -5\"), mdx(\"p\", null, \"we preserve the sign\"), mdx(\"h2\", null, \"7. UnSigned Right Shift ( >>> ):\"), mdx(\"p\", null, \"Unlike the Signed shift, the UnSigned one does not take sign bits into consideration. It just shifts all the bits to the right and pads the result with zeros\\nfrom the left . That means that for negative numbers, the result is always Positive. Signed and UnSigned right shifts have the same result for the postive numbers\"), mdx(\"p\", null, \"Ex : a =10\"), mdx(\"p\", null, \"a >>> 1 = 5\"), mdx(\"p\", null, \"a= -10\"), mdx(\"p\", null, \"a >>> 1 = 2147483643\"), mdx(\"strong\", null, \"Does NOT preserve the sign bit \"), mdx(\"p\", null, \"This operator shifts the first operand to the spectified number of bits ot the right . Excess bits shifted off to the right are \", mdx(\"strong\", null, \"discarded\"), \". Zero bits are shifted from the left . The sign bit becomes 0 ,so result is \", mdx(\"strong\", null, \"always non-negative\"), \".\"), mdx(\"h2\", null, \"8. UnSigned Left Shift ( <<< ):\"), mdx(\"p\", null, \"Unlike UnSigned Right shift , there is no \\\"<<<\\\" operator in Java , because the logical (<< ) and arithmetic left- shift (<<<) operations are identical.\"), mdx(\"p\", null, \"9 (base 10) : 00............1001 (base 2)\"), mdx(\"p\", null, \"9 >>> 2 (base 10) : 00............0010 (base 2) = 2 (base 10)\"), mdx(\"p\", null, \"However this is not the case for negative numbers\"), mdx(\"p\", null, \"For Ex: -9 >>> 2 yields 1073741821, which is different than -9 >> 2 (which yields -3)\"), mdx(\"p\", null, \"-9 (base 10) : 11.........110111 ( base 2)\"), mdx(\"p\", null, \"-9 >>> 2 (base 10 ): 0011............1101 (base 2) = 1073741821 (base 10)\"), mdx(\"table\", {\n    className: \"table-space table-border\"\n  }, mdx(\"tr\", null, mdx(\"th\", null, \"X\"), mdx(\"th\", null, \"Y\"), mdx(\"th\", null, \"X & Y\"), mdx(\"th\", null, \"X | Y\"), mdx(\"th\", null, \"X ^ Y\"), mdx(\"th\", null, \"~ (X) \")), mdx(\"tr\", null, mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"1\")), mdx(\"tr\", null, mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\")), mdx(\"tr\", null, mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"0\")), mdx(\"tr\", null, mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"1\"), mdx(\"td\", null, \"0\"), mdx(\"td\", null, \"0\"))));\n}\n;\nMDXContent.isMDXComponent = true;","slug":"ds/bit_manipulation"},"allMdx":{"edges":[{"node":{"slug":"ds/arrays","id":"8ab43cbe-bc50-5590-925e-1a45d7028386"}},{"node":{"slug":"ds/binary_addition","id":"26e70246-f787-5a17-baa8-9375bbacc879"}},{"node":{"slug":"ds/binary_divison","id":"ec6f892f-531e-5661-8a44-d08f906ecba5"}},{"node":{"slug":"ds/binary_operations","id":"b2e4a072-3108-546d-a06a-646e43d834cc"}},{"node":{"slug":"ds/binary_multiplication","id":"0b241fae-5107-5843-b21e-7a5ed393168f"}},{"node":{"slug":"ds/binary_substraction","id":"18f53266-039e-5e0c-af57-37c05a2e80ec"}},{"node":{"slug":"ds/linear_data_structures","id":"0d96c16b-7430-59aa-b81a-73e67dcfd3d2"}},{"node":{"slug":"ds/bit_manipulation","id":"03ba2551-8800-5f50-9724-3a84d09a5989"}},{"node":{"slug":"ds/intro","id":"67e2c78b-07c3-57cf-9984-08e405f77783"}},{"node":{"slug":"ds/roadmap","id":"f7856a33-93ca-5464-8e5a-d5dc41765650"}},{"node":{"slug":"ds/programming_language","id":"ac14fda6-ff3a-5306-964a-3ce25168a6f0"}},{"node":{"slug":"ds/number_systems","id":"12494952-aaee-5db2-91ef-4db461c5b95c"}},{"node":{"slug":"ds/time_space_complex","id":"b7695373-5e9c-56c5-9b7f-5a632785916f"}}]}},"pageContext":{"id":"03ba2551-8800-5f50-9724-3a84d09a5989","filePath":"/ds/","previousPostId":"ds/linear_data_structures","nextPostId":"ds/intro"}},
    "staticQueryHashes": ["2033233798"]}