logic

Fastest way to get sign in Java?

江枫思渺然 提交于 2019-12-03 09:27:13
I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a = ...; int sign = a >> 31; //0 for pos, 1 for neg sign = ~sign; //1 for pos, 0 for neg sign = sign << 1; //2 for pos, 0 for neg sign -= 1; //-1 for pos, 1 for neg -- perfect. Or more concisely: int sign = (~(a >> 31) << 1) - 1; Does this seem like a good approach? Will this work for all platforms, given endianness concerns (as MSB holds sign)? assylias Any

bitParity - Finding odd number of bits in an integer

元气小坏坏 提交于 2019-12-03 09:03:23
I have to create a function bitParity(int x) that takes an integer and returns 1 if there is an odd number of 0 's in the bit form of x , and 0 otherwise. Ex: bitParity(5) = 0, bitParity(7) = 1 However, this is difficult as I can only use bit operators on this problem ( ! ˜ & ˆ | + << >> are the only legal ones). That means, no loops, if-then , or anything of the sort. Constants can be used. So far, what I have doesn't work, but I figured that I should shift the bits of the integer 16 , 8 , and 4 times and XOR the remaining integers. Can anyone offer some advice? Thanks. This is properly

Create PDF of dynamic size with typography using UIView template(s)

二次信任 提交于 2019-12-03 08:58:06
I'm new but have managed to learn a lot and create a pretty awesome (I hope) app that's near completion. One of my last tasks is to create a PDF of dynamically generated user data. It has been the most frustrating part of this whole process as there is no real modern clear cut template or guide. Apple's documentation isn't very descriptive (and some parts I don't understand) and the Q/A here on stack and examples on Google all seem very case specific. I thought I almost had it by using a UIScrollView but the end result was messy and I couldn't get things to line up neat enough, nor did I have

Login system concept & logic?

守給你的承諾、 提交于 2019-12-03 07:57:42
I want to know the process which usually web apps follow to maintain login between multiple requests and also how they manage things using COOKIES. In my login form I am providing "Remember Me" feature. When user login then I check the username and password validity from database. If its valid then I check if "Remember me" is selected, if yes then storing username and password in session, encrypted format. And finally storing username and password in SESSION. When user navigates form one page to another, first I run login check script which checks if there is any value in cookies, then it

reverse the position of integer digits?

烂漫一生 提交于 2019-12-03 07:43:57
i have to reverse the position of integer like this input = 12345 output = 54321 i made this but it gives wrong output e.g 5432 #include <iostream> using namespace std; int main(){ int num,i=10; cin>>num; do{ cout<< (num%i)/ (i/10); i *=10; }while(num/i!=0); return 0; } Your loop terminates too early. Change }while(num/i!=0); to }while((num*10)/i!=0); to get one more iteration, and your code will work. Here is a solution int num = 12345; int new_num = 0; while(num > 0) { new_num = new_num*10 + (num % 10); num = num/10; } cout << new_num << endl; If you try it once as an example, you'll see

What are some practical applications of an FPGA?

Deadly 提交于 2019-12-03 07:08:12
问题 I'm super excited about my program powering a little seven-segment display, but when I show it off to people not in the field, they always say "well what can you do with it?" I'm never able to give them a concise answer. Can anyone help me out? 回答1: First: They don't need to have volatile memory. Indeed the big players (Xilinx, Altera) usually have their configuration on-chip in SRAM, so you need additional EEPROM/Flash/WhatEver(TM) to store it outside. But there are others, e.g. Actel is one

Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)

我是研究僧i 提交于 2019-12-03 06:26:09
问题 Write C/C++/Java code to convert given number into words. eg:- Input: 1234 Output: One thousand two hundred thirty-four. Input: 10 Output: Ten Does it require a complete switch case for digits 0 to 10. Adding "teen" after every number name (eg: 14: four "teen".) from 14 to 19. And than adding "ty" and the digits name for a number in the range 20 to 99. And so on. I think there must be some far better approach for solving this. C code is preferred. 回答1: #include<iostream> using namespace std;

How to calculate scores?

耗尽温柔 提交于 2019-12-03 06:23:59
This question is more related to logic than any programming language. If the question is not apt for the forum please do let me know and I will delete this. I have to write a logic to calculate scores for blogs for a Blog Award website. A blog may be nominated for multiple award categories and is peer-reviewed or rated by a Jury on a -1 to 5 scale (-1 to indicate a blog they utterly dislike). Now, a blog can be rated by one or more Jurors. One criterion while calculating final score for a blog is that if a blog is rated positively by more people it should get more weightage (and vice-versa).

How to determine if a date range occurs any time within another date range?

↘锁芯ラ 提交于 2019-12-03 06:23:17
问题 I have an Event table that specifies a date range with start_date and end_date fields. I have another date range, specified in code, that defines the current week as 'week_start' and 'week_end'. I'd like to query all Events for the week. The cases seem to be: Event begins and ends within the week Event begins before the week, but ends within the week Event begins within the week, but ends after the week Event begins before the week and also ends after the week Events that neither reside

highest palindrome with 3 digit numbers in python

瘦欲@ 提交于 2019-12-03 05:19:38
问题 In problem 4 from http://projecteuler.net/ it says: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers. I have this code here def isPalindrome(num): return str(num) == str(num)[::-1] def largest(bot, top): for x in range(top, bot, -1): for y in range(top,bot, -1): if isPalindrome(x*y): return x*y print largest(100,999) It should find the