division

Check if a number is divisible by 3 [closed]

人盡茶涼 提交于 2019-11-27 01:04:04
Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input "0": (0) output 1 inputs "1,0,0": (4) output 0 inputs "1,1,0,0": (6) output 1 This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I'll accept any coding language. Bonus points for a hardware implementation (verilog etc). Part a (easy): First input is the MSB. Part b (a little harder): First

What is the reason for having '//' in Python? [duplicate]

≡放荡痞女 提交于 2019-11-26 23:25:35
This question already has an answer here: What is the difference between '/' and '//' when used for division? 14 answers I saw this in someone's code: y = img_index // num_images where img_index is a running index and num_images is 3. When I mess around with // in IPython, it seems to act just like a division sign (i.e. one forward slash). I was just wondering if there is any reason for having double forward slashes? In Python 3, they made the / operator do a floating-point division, and added the // operator to do integer division (i.e. quotient without remainder); whereas in Python 2, the /

Perform integer division using multiplication [duplicate]

喜你入骨 提交于 2019-11-26 21:14:01
问题 This question already has answers here : Why does GCC use multiplication by a strange number in implementing integer division? (4 answers) Closed 10 months ago . Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9 = (12345678 * 0x1C71C71D) /

64/32-bit division on a processor with 32/16-bit division

半腔热情 提交于 2019-11-26 20:35:04
问题 My processor, a small 16-bit microcontroller with no FPU and integer math only has 16/16 division and 32/16 division which both take 18 cycles. At the moment I'm using a very slow software routine (~7,500 cycles) to do 64/32 division. Is there any way to use these division engines to calculate a 64/32 division? Similar to how I'm already using the 16x16 multiplier and adder to calculate 32x32 multiplies? I'm using C but can work with any general explanation on how it can be done... I'm hoping

Python-style integer division & modulus in C

笑着哭i 提交于 2019-11-26 20:34:00
问题 In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand: >>> (-41) / 3 -14 >>> (-41) % 3 1 However, in C and Java, signed integer division truncates towards 0, and signed integer modulus has the same sign as the first operand: printf("%d\n", (-41) / 3); /* prints "-13" */ printf("%d\n", (-41) % 3); /* prints "-2" */ What is the simplest and most efficient way in C to perform the same kind of division and

Which is better option to use for dividing an integer number by 2?

百般思念 提交于 2019-11-26 19:17:15
Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here x is an integer. Use the operation that best describes what you are trying to do. If you are treating the number as a sequence of bits, use bitshift. If you are treating it as a numerical value, use division. Note that they are not exactly equivalent. They can give different results for negative integers. For example: -5 / 2 = -2 -5 >> 1 = -3 (ideone) Cat Plus Plus Does the first one look like dividing? No. If you want to divide, use x / 2 . Compiler

Double value returns 0 [duplicate]

拥有回忆 提交于 2019-11-26 17:56:05
This question already has an answer here: Int division: Why is the result of 1/3 == 0? 15 answers Here's an example: Double d = (1/3); System.out.println(d); This returns 0, not 0.33333... as it should. Does anyone know? Firas Assaad That's because 1 and 3 are treated as integers when you don't specify otherwise, so 1/3 evaluates to the integer 0 which is then cast to the double 0 . To fix it, try (1.0/3) , or maybe 1D/3 to explicitly state that you're dealing with double values. If you have int s that you want to divide using floating-point division, you'll have to cast the int to a double :

Floor division with negative number

天涯浪子 提交于 2019-11-26 17:33:54
问题 The expression 6 // 4 yields 1 , where floor division produces the whole number after dividing a number. But with a negative number, why does -6 // 4 return -2 ? 回答1: The // operator explicitly floors the result. Quoting the Binary arithmetic operations documentation: the result is that of mathematical division with the ‘floor’ function applied to the result. Flooring is not the same thing as rounding to 0; flooring always moves to the lower integer value . See the math.floor() function:

Java int division confusing me

浪尽此生 提交于 2019-11-26 17:12:40
问题 I am doing very simple int division and I am getting odd results. This code prints 2 as expected: public static void main(String[] args) { int i = 200; int hundNum = i / 100; System.out.println(hundNum); } This code prints 1 as not expected: public static void main(String[] args) { int i = 0200; int hundNum = i / 100; System.out.println(hundNum); } What is going on here? (Windows XP Pro, Java 1.6 running in Eclipse 3.4.1) 回答1: The value 0200 is an octal (base 8) constant. It is equal to 128

Division in C++ not working as expected

北城余情 提交于 2019-11-26 15:27:33
I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0. #include <iostream> int main(int argc, char** argv) { double f=3/5; std::cout << f; return 0; } What am I missing? You are missing the fact that 3 and 5 are integers, so you are getting integer division. To make the compiler perform floating point division, make one of them a real number: double f = 3.0 / 5; It doesn't need to be .0 , you can also do 3./5 or 3/5. or 3e+0 / 5 or 3 / 5e-0 or 0xCp-2 / 5 or... There only needs to be an indicator involved so that the