How to turn this bitwise operation into something that can be understood by an average Joe? - bit-manipulation

I have code that I am refactoring now:
this.$rangeLabel
.html(this.label || pos)
.css({
left: pos,
marginLeft: -this.$rangeLabel.width() >> 1
});
I don't know why this is used there but I am sure it's just someone tried to look clever, though I don't know the purpose of it and how to convert it it to something more readable?
-this.$rangeLabel.width() >> 1

Related

Can I use return to return to the start of an if statement?

I'm currently programming some homework, and we have to make a program that turns a hindu-arabic numeral into a roman numeral. I've already made the first part, where my teacher said we had to make sure the number is in between 1 and 3999. My algorithm so far is like this:
if (num-1000) > 0 {
add M to output
num -= 1000
return if
}
else {
(repeat for other digits, aka 500, 100, 50, and so on)
}
The problem is, I don't know if it's even possible. All the Stack Overflow pages I've seen say that I should use while statements for this, but since we haven't tackled while statements yet (even though I've self-learned it) we can't use while loops. So, can I use return to return to the start of an if statement?
What you are describing is a while. You can also achieve that with a go to but using that at this stage of learning would inspire some very very bad habits so I wholeheartedly discourage it. Another way you could do this is with recursion but that is an even more advance topic.
Given your restrictions (no loops, a number between 1 and 3999) I think you are supposed to use a bunch of ifs. Something like this pseudocode:
if (n >= 3000)
add 'M'
else if (n >= 2000)
add 'MM'
else if (n >= 1000)
add 'MMM'
n = n % 1000;
if (n >= 900)
add 'CM'
// and so on

How do bitwise operations work on expressions within variable assignments in C/C++?

I'm learning reverse engineering, and I have the following snippet which I am trying to make sense of:
var = strcmp("C:\\Windows\\System32\\svchost.exe", pe.szExeFile);
if (var)
var = -(var < 0) | 1;
if (var)
{
// additional code here
}
I think I understand most of what is going on here, but I'm confused about the purpose of the
var = -(var < 0) | 1; line. I'm only very vaguely familiar with C/C++, so I'm having a hard time wrapping my head around what this line does.
I understand that it's a bitwise OR, but I'm unsure how the -(var < 0) works. Is the expression inside the parentheses evaluated to a 1 or 0 and then the negative is applied and the OR? Is it evaluated as a boolean? If so, how does the | work on a boolean?
Or am I totally missing the point here?
strcmp() returns one of three possible results:
< 0
0
> 0
Assumed common two's complement, after the first if the variable var will be
-1 for the former "< 0"
0 for the former "= 0"
+1 for the former "> 0"
However, the second if will be taken only if var is non-zero.
The "mysterious" first if has no effect, as far as the source is concerned that you show.

Reverse For loop skips over completely if there is only one entry?

I have an array I am reverse looping through. However, it seems if there is only one entry in my array, it doesn't loop at all.
for (uint8 i = fileNames.Num() - 1; i --> 0;)
{
//Do stuff
}
Can anyone tell me why that is? Or what I can do to fix the loop conditions?
The code you posted means the same thing as the following:
uint8 i = filenames.Num() - 1
while ( (i--) > 0) {
//do stuff
}
If filename.Num() is 1 then the loop body does not execute because 1 - 1 is not greater than zero.
This expression i --> 0 is not idiomatic C++ and has no place in real code. It is a programming language pun, and to some extent a joke someone posted on Usenet at the expense of newbies akin to sky hooks or snipe hunts in other social domains e.g. "Hey did you know about the secret operator in C++???"

Statements in C++

In C++ want to write something like this
int Answer;
if (Answer == 1 || Answer == 8 || Answer == 10)
and so on, is it any way to make code shorter without repeating variable always?
Try:
switch (Answer) {
case 1: // fall through
case 8: // fall through
case 10:
// ... do something
break; // Only need if there are other case statements.
// Leaving to help in mainenance.
}
For readability I'd encapsulate the logic in descriptively-named functions. If, say, your answers are things with a particular color, and answers 1, 8, and 10 are green things, then you can write that logic as
bool ChoiceIsGreen(int answer)
{
return (answer == 1 || answer == 8 || answer == 10);
}
Then your function becomes
if (ChoiceIsGreen(Answer))
{
// offer some soylent green
}
If you have a lot of choices like this, I can see it getting hard to read if you have a lot of raw numbers all over the place.
If and only if you need to optimise for code size manually, and Answer is guaranteed to be positive and less than the number of bits in an int, you might use something like
if ( ( 1 << Answer ) & 0x502 )
But normally you don't want to obscure your logic like that.
You could put the values into a container and search the container.
Sounds like a std::set would be a wise choice:
if answer is in the set of (1, 8, 10) then do....
Remember that a std::set must be initialized during run-time, unlike numeric constants or an array of numeric constants. Before making any performance changes, first get the program working correctly, then profile if necessary, that is only if the program demands performance optimization.

JRXML operator error

I'm getting this error when compiling my JRXML file in iReport 3.1.2:
com.jaspersoft.ireport.designer.errorhandler.ProblemItem#f1cdfb The operator > is undefined for the argument type(s) java.lang.Integer, java.lang.Integer net.sf.jasperreports.engine.design.JRDesignExpression#eb40fe
The only place in my entire report where I use the operator > is here:
<parameter name="dynamicSectionCondition" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[($P{sectionId} != null && $P{sectionId} > new Integer("0")) ? new String("AND loctn_sctn_id = " + $P{sectionId}) : new String("")]]>
</defaultValueExpression>
</parameter>
Google hasn't been my friend on that one. Any Jasper template expert out there has a vague idea on what's going on?
Figured out how to compare 2 Integers in the JRXML. Instead of doing this:
$P{sectionId} > new Integer("0")
The solution is this:
$P{sectionId}.compareTo(new Integer("0")) != 0
That looks a little convoluted but it works for me. :)
Although this monologue between you and yourself Sir Lancelot, is ages old and originating in medival times, I have to give a fair warning to travelers which might want to take this ancient lore to their library.
If you want to know if $P{sectionId} is bigger than new Integer("0"), then your solution would yield true, even when $P{sectionId} is smaller than new Integer("0").
Cause:
(new Integer("-5")).compareTo(new Integer("0"))
would result in:
-1
which would not be 0 as in:
$P{sectionId}.compareTo(new Integer("0")) != 0
and therefore the condition would be also true.
Just use:
$P{sectionId}.compareTo(new Integer("0")) == 1
Remember:
x.compareTo(0) == 1 (if x > 0)
x.compareTo(0) == 0 (if x == 0)
x.compareTo(0) == -1 (if x < 0)
Sorry if you already know the basics and your 2nd post was just to show a common way of solving the problem, but I thought if a newbie is reading your first post, he/she maybe assumes, that one is able to find numbers bigger than 0, with the posted solution.