Uncrustify - How do I align trailing comments by two-space gap? - uncrustify

After I applied Atom Beautify on my code using Uncrustify, the trailing comments are aligned as follow:
unsigned int redLEDValue = 0; // redLED
unsigned int blueLEDValue = 0; // blueLED
unsigned int greenLEDValue = 0; // greenLED
unsigned int redSensorValue = 0; // redSensor
unsigned int blueSensorValue = 0; // blueSensor
unsigned int greenSensorValue = 0; // greenSensor
There is only once space between the semicolon and the trailing comment, but I want to have at least 2 spaces between them. I'm using Atom 1.12.9, Atom Beautify 0.29.16, and Uncrustify 0.64. Here is my Uncrustify config.
Is there any way to increase the gap? I've already set align_right_cmt_span = 4 but no hope. Thanks in advance.

You have to enable adding spaces first with the option:
sp_before_tr_emb_cmt = add
sp_before_tr_emb_cmt: Controls the spaces before a trailing or
embedded comment
Than you can adjust the amount of spaces with:
sp_num_before_tr_emb_cmt = 2
sp_num_before_tr_emb_cmt: Number of spaces before a trailing or
embedded comment
Edit:
Uncrustify has also an aligning function for trailing comments which is applied after the spacing functionalities. The aligning function is controlled by those three options:
align_right_cmt_span = 1 # unsigned number
The span for aligning comments that end lines (0=don't align)
align_right_cmt_gap = 10 # number
If a trailing comment is more than this number of columns away from the text it follows,
it will qualify for being aligned. This has to be > 0 to do anything.
A negative value [will] force comments which are stuck to the previous token
(gap=0) into alignment with the others.
align_right_cmt_at_col = 80 # unsigned number
Align trailing comment at or beyond column N; 'pulls in' comments as a bonus side effect (0=ignore)
Note:
The functionality is enabled if align_right_cmt_span is greater
than 0.
Contrary to its description align_right_cmt_gap is
enabled if it is 0. All numeric options are initially set to 0 as default values.
If align_right_cmt_at_col is less than the column of the last char before the beginning of the comment the comment will be indented to one space after the last char.

Related

16-digit number manipulation on a 32-bit programming language

I have a simple problem, but because this "programming language" I am using is 32-bit and only supports basic functions such as addition, subtraction, multiplication, division, and concatenation (literally that's it), I am having some trouble.
For the input, I have a 16 digit number like so: 3334,5678,9523,4567
I want to then subtract 2 other random 16 digit numbers from this number and check if the first and last digits are 1.
For example, if the two other numbers are 1111,1111,1111,1111 and 1234,5678,9123,4565.
My final number would be: 0988,8888,9288,8891.
Here, the last number is 1, but the first number is 0, so the test would fail.
The issue is with 32-bit systems, there are massive errors due to not enough precision provided by the bits. What are some ways to bypass this issue?
If you're using a language like C or Java you should be able to use a long to create a 64 bit integer. If that's not possible you could divide the numbers into two 32 bit numbers, one to hold the upper half and one to hold the lower half.
Something like this:
//Each half is 8 digits to represent 8 of the 16
//Because of this each half should be less than 100000000
int upperHalf = 33345678;
int lowerHalf = 95234567;
//randomInt represents a function to generate a random
//integer equal to or greater than 0 and less than the
//argument passed to it
int randUpperHalf = randomInt(100000000);
int randLowerHalf = randomInt(100000000);
int lowerHalf = lowerHalf - randLowerHalf;
//If lowerHalf was a negative number you need to borrow from the upperHalf
if (lowerHalf < 0) {
upperHalf = upperHalf - 1;
lowerHalf = lowerHalf + 100000000;
}
upperHalf = upperHalf - randUpperHalf;
//Check that the first and last digits are 1
if ((upperHalf / 100000000) == 1 && (lowerHalf % 10) == 1) {
//The first and last digits are 1
}
Edit: Comments have been added to explain the code better. (lowerHalf % 2) == 1 has been changed to (lowerHalf % 10) == 1 and should now be able to tell if the number ends in a 1.

iOS: Using Accelerate Framework to append / remove a column or row from a matrix?

I've looked through the vDSP and BLAS reference docs, and can't seem to find anything on appending / removing a row or column from a matrix. I'm currently using for-loops, but would rather use an accelerate function if one exists.
We can use vDSP_mmov(1) or vDSP_mmovD(2). Below sample to append 1 row at end.
//sanple to add 1 row
float dst[4][4] = { 1,2,3,4, 5,6,7,8, 9,10,11,12 } ; //last row empty
float src[1][4] = { 13,14,15,16 };
//to fill last row
int numColumnsToCopy = 4;
int numRowsToCopy = 1;
int numColsinDst = 4;
int numColsinSrc = 4;
vDSP_mmov(src, &dst[3][0], numColumnsToCopy, numRowsToCopy, numColsinSrc, numColsinDst );
The same sample could be tweaked to append/remove rows/columns at the end. Though you could overwrite rows/columns in middle, I am not sure if you could append/remove a row/column in middle of matrix as that will need shifts. You might need to split then.
Though there is no harm in using for one off runs, you might not get speed benefits you are looking for. For repetitive runs, these frameworks help.

Riemann Sum Estimation

I'm trying to calculate the value of n that solves the problem below. I am not exactly sure where I am messing up. I tried using a do while loop also, but I am having trouble figuring out the logic error. Could anyone assist?
If S = √ (6*( 1+1/2^2+1/3^2 +1/4^2 + 1/5^2 + ... ) ) = (pi^2)/6, after how many terms will the sum be equal to PI to 6 decimal places. PI to 6 decimal places is 3.141592. The relevant part of my code is shown below:
double s = 0;
for(int n=1;abs(sqrt(6*s) - 3.141592) >= pow(10,-6);n++) {
s += (1/(pow(n,2)));
NSLog(#"%i",n);
}
int abs(int i)
computes the absolute value of an integer. Therefore in
abs(sqrt(6*s) - 3.141592)
the floating point number sqrt(6*s) - 3.141592 is converted to an int
first, which gives zero as soon as the absolute value of this number is less than one.
You want to use fabs() instead.

Special characters to int, and then back

So what I want, is for example to convert the letter 'a' into 97 (such as it is in the ASCII table), and then convert 67 into 'a'.
I actually perform a load of mathematics and stuff to the letter, treating it as binary number - so the transition is necessary.
However for special characters it is not working nicely.
char c = 'ÿ';
int i = int(c);
wchar_t wTemp = static_cast<wchar_t>(i);
wchar_t* w = &wTemp;
String^ newI = gcnew String(w);
That symbol is just a random one I found in an image (the type of character that will need to be read). It just comes out as a completely different symbol. I have no idea why, or what to do?
Characters above 0x7f (127) are probably converting to negative integer values. Maybe change c to unsigned:
unsigned char c = 'ÿ';
int i = c;
Your code doesn't look quite right to me though I didn't run it. Here is a good example from MSDN how to convert from and to wchar_t:
http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx
I don't believe there is anything special about 'special' characters.

Getting the Leftmost Bit

I have a 5 bit integer that I'm working with. Is there a native function in Objective-C that will let me know which bit is the leftmost?
i.e. I have 01001, it would return 8 or the position.
Thanks
You can build a lookup table, with 32 elements: 0, 1, 2, 2, 3, etc.
This is effectively the same operation as counting he number of leading 0s. Some CPUs have an instruction for this, otherwise you can use tricks such as those found in Hacker's Delight.
It's also equivalent to rounding down to the nearest power of 2, and again you can find efficient methods for doing this in Hacker's Delight, e.g.
uint8_t flp2(uint8_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
return x - (x >> 1);
}
See also: Previous power of 2
NSInteger value = 9;
NSInteger shift = 1;
for(NSInteger bit = value; bit > 1; bit = value >> ++shift);
NSInteger leftmostbit = 1 << shift;
Works for every number of bits.
If you don't want to use a table lookup, I would use 31 - __builtin_clz(yourNumber).
__builtin_clz( ) is a compiler intrinsic supported by gcc, llvm-gcc, and clang (and possibly other compilers as well). It returns the number of leading zero bits in an integer argument. Subtracting that from 31 gives you the position of the highest-order set bit. It should generate reasonably fast code on any target architecture.
Stanford Bit Twiddling Hacks have lots of examples of how to accomplish this.
If you mean the value of whatever bit is in position five from the right (the "leftmost" of a five-bit value), then:
int value = 17;
int bit = (value >> 4) & 1; // bit is 1
If you mean the position of the leftmost bit that is 1:
int value = 2;
int position;
for (position = 0; position < 5; position++) {
int bit = (value >> position) & 1;
if (bit == 1)
break;
}
// position is 1
Position will be 0 for the bit furthest to the right, 4 for the leftmost bit of your five-bit value, or 5 if all bits where zero.
Note: this is not the most effective solution, in clock cycles. It is hopefully a reasonably clear and educational one. :)
To clear all bits below the most significant bit:
while ( x & (x-1) ) x &= x - 1;
// 01001 => 01000
To clear all bits above the least significant bit:
x &= -x;
// 01001 => 00001
To get the position of the only set bit in a byte:
position = ((0x56374210>>(((((x)&-(x))*0x17)>>3)&0x1C))&0x07);
// 01000 => 3
In libkern.h there is a clz function defined to count leading zeros in a 32 bit int. That is the closest thing to a native Objective-C function. To get the position of the most significant bit in an int:
position = 31 - clz( x );
// 01001 => 3
I don't know objective C but this is how I would do it in C.
pow(2, int(log2(Number))
This should give you the left most 1 bit value.
PLEASE SEE STEPHEN CANON'S COMMENT BELOW BEFORE USING THIS SOLUTION.
With VC++ have a look at
_BitScanReverse/(64) in