twos-complement

how to do two complement multiplication and division of integers?

别说谁变了你拦得住时间么 提交于 2019-11-30 03:45:06
I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be

why -3==~2 in C#

落花浮王杯 提交于 2019-11-29 23:36:26
Unable to understand. Why output is "equal" code: if (-3 == ~2) Console.WriteLine("equal"); else Console.WriteLine("not equal"); output: equal Because two's complement bit-arithmetic makes it so Cribbed from the wikipedia page and expanded: Most Significant Bit 6 5 4 3 2 1 0 Value 0 0 0 0 0 0 1 1 3 0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 0 -2 1 1 1 1 1 1 0 1 -3 1 1 1 1 1 1 0 0 -4 So you get: 0 0 0 0 0 0 1 0 = 2 1 1 1 1 1 1 0 1 = -3 And as you can see, all the bits are flipped, which is what the bitwise NOT operator ( ~ ) does. James Wiseman This

Convert two's complement to sign-magnitude

无人久伴 提交于 2019-11-29 18:06:18
I need to convert from two's complement to sign-magnitude in C using only the operators ! ~ & ^ | + << >> My approach is to find sign: int sign = !(!(a>>31)); basically, if sign == 1 . I want to flip the number and add 1 else just want to display the number. The thing is I can't use any loops, if statements etc. This is what I'm working on: int s_M = ((((a+1)>>31)^sign)+1)&sign; any suggestions? From http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs int const mask = v >> 31; unsigned int r = (v + mask) ^ mask; Gives the absolute value (magnitude). If you wish to add the sign bit

two's complement

只谈情不闲聊 提交于 2019-11-29 17:09:42
As far as I know, the two's complement algo is: 1.Represent the decimal in binary. 2.Inverse all bits. 3.Add 1 to the last bit. For the number 3 , which its representation is: 0000000000000011 the result of the two's complement would be 1111111111111101 which is -3 . So far so good. But for the number 2 which its representation is 0000000000000010 the result of the two's complement would be 1111111111111101 , which isn't 2 but -3. What am I doing wrong? 0...0010 // 2 1...1101 // Flip the bits 1...1110 // Add one It works for negative too: 1...1110 // -2 0...0001 // Flip the bits 0...0010 //

Java two's complement binary to integer [duplicate]

旧街凉风 提交于 2019-11-29 14:31:12
This question already has an answer here: 2's complement hex number to decimal in java 3 answers I know that converting a decimal to binary with Integer.toBinaryString(355) = 0000000101100011 and Integer.toBinaryString(-355) = 1111111010011101 (where I take the lower 16 bits of the 32 bit result). What I would like to do is the other way and take a 16-bit twos's complement binary string and to convert to decimal. i.e. 0000000000110010 = 50 1111111111001110 = -50 Rather than 1111111111001110 = 65486 How would I do this? You need to read the result into short . short res = (short)Integer

Java binary literals - Value -128 for byte

流过昼夜 提交于 2019-11-29 10:28:42
Since SE 7 Java allows to specify values as binary literal. The documentation tells me 'byte' is a type that can hold 8 Bit of information, the values -128 to 127. Now i dont know why but i cannot define 8 bits but only 7 if i try to assign a binary literal to a byte in Java as follows: byte b = 0b000_0000; //solves to the value 0 byte b1 = 0b000_0001; //solves to the value 1 byte b3 = 0b000_0010; //solves to the value 2 byte b4 = 0b000_0011; //solves to the value 3 And so on till we get to the last few possibilitys using those 7 bits: byte b5 = 0b011_1111; //solves to the value 63 byte b6 =

Performing arithmetic operations in binary using only bitwise operators [duplicate]

徘徊边缘 提交于 2019-11-29 08:55:15
Possible Duplicate: How can I multiply and divide using only bit shifting and adding? I have to write functions to perform binary subtraction, multiplication, and division without using any arithmetic operators except for loop control. I've only written code in Java before now, so I'm having a hard time wrapping my head around this. Starting with subtraction, I need to write a function with prototype int bsub(int x, int y) I know I need to convert y to two's complement in order to make it negative and add it to x, but I only know how to do this by using one's complement ~ operator and adding 1

how to take twos complement of a byte in c++

久未见 提交于 2019-11-29 04:41:26
I am looking at some c++ code and I see: byte b = someByteValue; // take twos complement byte TwosComplement = -b; Is this code taking the twos complement of b? If not, What is it doing? This code definitely does compute the twos-complement of an 8-bit binary number, on any implementation where stdint.h defines uint8_t : #include <stdint.h> uint8_t twos_complement(uint8_t val) { return -(unsigned int)val; } That is because, if uint8_t is available, it must be an unsigned type that is exactly 8 bits wide. The conversion to unsigned int is necessary because uint8_t is definitely narrower than

Adding and subtracting two's complement

无人久伴 提交于 2019-11-29 02:27:34
Using six-bit one's and two's complement representation I am trying to solve the following problem: 12 - 7 Now, i take 12 in binary and 7 in binary first. 12 = 001100 - 6 bit 7 = 000111 - 6 bit Then, would I then flip the bit for two's complement and add one? 12 = 110011 ones complement + 1 ------- 001101 7 = 111000 ones complement + 1 --------- 111001 then, add those two complement together 001101 +111001 ------- 1000110 = overflow? discard the last digit? If so I get 5 Now, if I have a number like -15 + 2 I would then add a sign magnitude on the MSB if it's a zero? like: -15 = 001111 6 bit

Why byte b = (byte) 0xFF is equals to integer -1?

限于喜欢 提交于 2019-11-29 01:14:15
Why byte b = (byte) 0xFF is equal to integer -1 ? Ex: int value = byte b = (byte) 0xFF; System.out.println(value); it will print -1 ? cletus Bytes are signed in Java. In binary 0x00 is 0, 0x01 is 1 and so on but all 1s (ie 0xFF) is -1, 0xFE is -2 and so on. See Two's complement , which is the binary encoding mechanism used. b is promoted to an int in determining which overload of system.out.println to call. All bytes in Java are signed. The signed byte 0xff represents the value -1 . This is because Java uses two's complement to represent signed values. The signed byte 0xff represents -1