2449 views|16 replies

825

Posts

3

Resources
The OP
 

Integer division problem [Copy link]

 

Two integers a=3, b=2;

The integer c=a/b is obtained by dividing the two numbers (the value of c is 1, which should actually be 1.5).

If the integer c obtained by division is shifted one place to the left, will the value of c be 10 or 15?

This post is from 51mcu

Latest reply

According to other people's explanation, it should be ×2   Details Published on 2022-6-8 09:21
 

2w

Posts

0

Resources
2
 

"If the integer c obtained by division is shifted one position to the left, the value of c is 10 or 15?"

Computers use binary. Shifting left by one bit is shifting one bit in binary.

This post is from 51mcu

Comments

"If the integer c obtained by division is shifted one position to the left, will the value of c be 10 or 15?" There is a problem with my description here. It is not left shift, but the quotient c=3/2. The obtained c*10 can be 15. Because the language used is Verilog, there is no floating point syntax. But the problem has been solved now.  Details Published on 2022-6-11 14:26
 
 
 

6788

Posts

2

Resources
3
 

If we multiply the integer c obtained by division by 10, the value is 10. If we shift it left by one position, the value is 2.

This post is from 51mcu

Comments

If two binary numbers are divided and the quotient is one decimal place, is it OK to shift the quotient right and then find the remainder by 10? How many places should we shift right?  Details Published on 2022-6-7 09:44
 
 
 

6841

Posts

11

Resources
4
 
It depends on what type you defined ab as. If it is uint8_t c=a/b, that is, c = 1. Shift left one bit c = 2
This post is from 51mcu
 
 
 

825

Posts

3

Resources
5
 
wangerxian posted on 2022-6-6 19:19 [If the integer c obtained by division is multiplied by 10, it is 10. Shifting it left by one place is 2.

If two binary numbers are divided and the quotient is one decimal place, is it OK to shift the quotient right and then find the remainder by 10? How many places should we shift right?

This post is from 51mcu

Comments

Floating point types cannot be shifted left or right!  Details Published on 2022-6-7 13:03
Floating point types cannot be shifted left or right!  Details Published on 2022-6-7 13:03
 
 
 

143

Posts

1

Resources
6
 

I don't know what you are trying to do. Binary division 3/2, assuming 4 bits, 0011/0010, the quotient is 0001, the remainder is 0001

This post is from 51mcu

Comments

I encountered this when writing Verilog. Verilog cannot represent decimals, so I want to ask if c=3/2 directly becomes the integer 1. If so, the decimal part cannot be obtained, and another algorithm must be used.  Details Published on 2022-6-11 14:21
 
 
 

1792

Posts

0

Resources
7
 

There is a way to do it. If you want to get a decimal place, you can multiply the divisor by 10 before doing the division. This is equivalent to storing a variable that is 10 times the actual value. You can then process it accordingly when displaying or doing other calculations.

a*10/b=15

This post is from 51mcu
 
 
 

1792

Posts

0

Resources
8
 

In addition, you can use the operator % to find the remainder: 3%2=1

This post is from 51mcu
 
 
 

6788

Posts

2

Resources
9
 
1nnocent posted on 2022-6-7 09:44 If two binary numbers are divided and the quotient is one decimal place, is it okay to shift the quotient right and then find the remainder by 10? How many places should be shifted right?

Floating point types cannot be shifted left or right!

This post is from 51mcu
 
 
 

6788

Posts

2

Resources
10
 
1nnocent posted on 2022-6-7 09:44 If two binary numbers are divided and the quotient is one decimal place, is it okay to shift the quotient right and then find the remainder by 10? How many places should be shifted right?

And the remainder can only be an integer.

This post is from 51mcu
 
 
 

7462

Posts

2

Resources
11
 

Test it out

This post is from 51mcu

Comments

Problem solved   Details Published on 2022-6-11 14:22
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6069

Posts

4

Resources
12
 

Serves 2

c =a/b; //==> c=1

c <<=1;// ==>c=2

If you want to get the actual data of 1.5 (which cannot be represented by integers unless double precision, floating point);

Just use floating point a, b, c all use floating point, the conclusion is 1.5

If you want to get a number like 15, you can do:

a =a*10;

c = a/b;

At this time, c is 15

If you want higher precision

You can use a*100 a*1000, but you have to consider not overflowing.

This post is from 51mcu

Comments

The Verilog language is used, and there is no floating point syntax  Details Published on 2022-6-11 14:22
 
 
 

1582

Posts

0

Resources
13
 

According to other people's explanation, it should be ×2

This post is from 51mcu
 
 
 

825

Posts

3

Resources
14
 
jimtien posted on 2022-6-7 11:17 I don't know what you are trying to do, binary division 3/2, assuming 4 bits, 0011/0010, the quotient is 0001, the remainder is 0001

I encountered this when writing Verilog. Verilog cannot represent decimals, so I want to ask if c=3/2 directly becomes the integer 1. If so, the decimal part cannot be obtained, and another algorithm must be used.

This post is from 51mcu
 
 
 

825

Posts

3

Resources
15
 
damiaa posted on 2022-6-8 09:05 is 2 c =a/b; //==> c=1 c <<=1;// ==>c=2 If you want to get the actual data of 1.5...

The Verilog language is used, and there is no floating point syntax

This post is from 51mcu
 
 
 

825

Posts

3

Resources
16
 

Problem solved

This post is from 51mcu
 
 
 

825

Posts

3

Resources
17
 
maychang posted on 2022-6-6 18:58 "If the integer c obtained by division is shifted one bit to the left, the value of c is 10 or 15?" Computers use binary. Shifting one bit to the left is shifting binary...

"If the integer c obtained by division is shifted one position to the left, the value of c is 10 or 15?"

There is a problem with my description here. It is not a left shift, but the quotient c=3/2. The obtained c*10 cannot be 15, because the language used is Verilog, which does not have floating point syntax. But the problem has been solved now.

This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list