<!DOCTYPE html>

<html>
<head>
	<title>Bitwise Calculator</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<style>
		/* Style for the calculator buttons */
		button {
			font-size: 1.5rem;
			margin: 0.5rem;
			padding: 0.5rem;
			border: 1px solid #000;
			background-color: #fff;
			border-radius: 0.25rem;
			cursor: pointer;
			min-width: 3rem;
		}
		/* Style for the calculator screen */
		input[type=text] {
			font-size: 2rem;
			margin: 0.5rem;
			padding: 0.5rem;
			border: 1px solid #000;
			border-radius: 0.25rem;
			text-align: right;
			width: 100%;
		}
	</style>
</head>
<body>
	<div>
		<!-- Calculator screen -->
		<input type="text" id="result" disabled>
		<!-- Calculator buttons -->
		<button onclick="append('0')">0</button>
		<button onclick="append('1')">1</button>
		<button onclick="operate('&')">AND</button>
		<button onclick="operate('|')">OR</button>
		<button onclick="operate('^')">XOR</button>
		<button onclick="operate('<<')">Left Shift</button>
		<button onclick="operate('>>')">Right Shift</button>
		<button onclick="clearAll()">Clear</button>
	</div>
	<script>
		// Initialize the calculator screen with 0
		document.getElementById("result").value = 0;

		// Append a digit to the calculator screen
		function append(digit) {
			// Get the current value of the calculator screen
			var currentValue = document.getElementById("result").value;

			// If the current value is 0, replace it with the new digit
			if (currentValue === "0") {
				document.getElementById("result").value = digit;
			} else { // Otherwise, append the new digit to the current value
				document.getElementById("result").value += digit;
			}
		}

		// Perform a bitwise operation on the current value and the new value entered by the user
		function operate(operator) {
			// Get the current value of the calculator screen
			var currentValue = parseInt(document.getElementById("result").value, 2);

			// Get the new value entered by the user
			var newValue = parseInt(prompt("Enter a binary value:"), 2);

			// Perform the bitwise operation based on the operator chosen by the user
			switch (operator) {
				case "&":
					document.getElementById("result").value = (currentValue & newValue).toString(2);
					break;
				case "|":
					document.getElementById("result").value = (currentValue | newValue).toString(2);
					break;
				case "^":
					document.getElementById("result").value = (currentValue ^ newValue).toString(2);
					break;
				case "<<":
					document.getElementById("result").value = (currentValue << newValue).toString(2);
					break;
				case ">>":
					document.getElementById("result").value = (currentValue >> newValue).toString(2);
					break;
			}
		}

		// Clear the calculator screen
		function clearAll() {
			document.getElementById("result").value = 0;
		}
	</script>
</body>
</html>

Comments

Popular posts from this blog

Disclaimer gernater tool

Language translator tool