N
nish.sinnadurai
Hi,
I have two code snippets here and I'm wondering which is better, and
whether or not there is a better way to do the following:
I want to fill in an unsigned int variable with n 1s. Example: n = 4
--> var = 00...001111 in binary.
So,
Method 1:
var = 0;
var = pow(2, n) - 0.9999;
Method 2:
var = 0;
for(i = 0; i < n; i++)
var = var << 1 + 1;
---
I'm not quite sure which method is better (wrt to computational time),
or if there is a more elegant way to do this.
Can anyone help me out here?
Thanks!
Nish
UW09
I have two code snippets here and I'm wondering which is better, and
whether or not there is a better way to do the following:
I want to fill in an unsigned int variable with n 1s. Example: n = 4
--> var = 00...001111 in binary.
So,
Method 1:
var = 0;
var = pow(2, n) - 0.9999;
Method 2:
var = 0;
for(i = 0; i < n; i++)
var = var << 1 + 1;
---
I'm not quite sure which method is better (wrt to computational time),
or if there is a more elegant way to do this.
Can anyone help me out here?
Thanks!
Nish
UW09