表示已知大小变量的表达式结果的最小位数?

时间:2016-10-28 18:47:08

标签: c memory assembly binary bit

我试图详细了解计算机如何存储内存。所以我找到了有关如何表示二进制数或十六进制数的信息,但我发现了一个问题:

// Script intialized by Gulp
const path = require('path')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()

// Import registration and login functionality from database.js
const database = require('./database')

// Configuration
let jsonParser = bodyParser.json()
app.use(jsonParser)
app.use(express.static(path.join(__dirname, '../../docs')))
app.use(express.static(path.join(__dirname, '../../src')))

app.set('port', process.env.PORT || 8080);
app.set('view engine', 'pug')
app.set('views', 'src/pages')

app.listen(app.get('port'))
console.log('Listening on port: ' + app.get('port'))

// Routing
app.get('/', function(req, res) {
    console.log('Welcome!')
    res.render('index');
})

app.get('/login', function(req, res) {
    console.log('Uhh, it looks like something went wrong here')
    res.end()
})

// Link with database.js to link username and password attributes
app.post('/login', jsonParser, function(req, res) {
    console.log(req)
    console.log(req.body)
    //database.login('ThisIsATestAccountBoi', 'TestPassword')
    //console.log('Username: ' + username + '\n' + 'Password: ' + password)
    res.end()
})

所以我的想法是:我是否必须分别考虑所有三个整数并对最小的数字进行数学处理,例如0 * 0 + 0

或者我是否需要考虑问题导致的另一个变量n?

我在思考这个问题时遇到了一些麻烦。

3 个答案:

答案 0 :(得分:7)

所需的位数为n + n。以8位和最大无符号值为例:

255 * 255 + 255 = 65280

结果小于65536,这需要超过16位。

答案 1 :(得分:3)

n位无符号数的最大值为2 n - 1。

的最大结果
  

a * b + c

将是:

  

(2 n - 1)*(2 n - 1)+(2 n - 1)

     

(2 2n - 2 n - 2 n + 1)+(2 n - 1)

     

2 2n - 2 n

因此,

  

2 2n - 2 n

小于最大(2 * n)位无符号数:2 2n - 1,(2 * n)位足以代表任何答案。

如何编码的示例

uint64_t ab_plus_c_bad(uint32_t a, uint32_t b, uint32_t c) {
  return a*b + c;  // potential overflow with product.
}

uint64_t ab_plus_c_good(uint32_t a, uint32_t b, uint32_t c) {
  // a is widened before the multiplication
  return (uint64_t) a*b + c;  // no product overflow
}

答案 2 :(得分:1)

可以表示的最大值是pow(2,n)-1。 并且你的方程得到(pow(2,n)-1)*(pow(2,n)-1)+(pow(2,n)-1)

所以要找出需要多少位

n |max val.| max eqn.   | req'd bits 
--|--------|------------|-----------
1 |   1    |  1*1+1=2   | 2
2 |   3    |  3*3+3=12  | 4
3 |   7    |  7*7+7=56  | 6
4 |   15   |15*15+15=240| 8
5 |   31   |31*31+31=992| 10
...see the pattern

要想明白任何 n,请使用以下内容:

//This uses double since the loss of precision will be insignificant
//but covers a large range of n.
//There are more efficient ways for small values of n.
//long double could be used instead for REALLY large values of n
#include <math.h>
#define n 999UL

//the highest unsigned value that can be represented by n bits is:
double max_value = pow(2.0, (double)n) - 1.0;

//figure out the max value you can get with your equation
double max_eqn = max_value * max_value + max_value;

//take the log base 2 of that and round it up to get max bits
//note that log base x of n is equal to log(n)/log(x);
unsigned long req_bits = (unsigned long) ceil(log(max_eqn)/log(2.0));

printf("The maximum required number of bits for %ul bits is %ul", n, req_bits);