integer

C++ Programs return int type, so why does return -1 return 255? [duplicate]

六眼飞鱼酱① 提交于 2019-12-23 12:21:09
问题 This question already has answers here : Return value range of the main function (7 answers) Closed 4 years ago . Note that I am running a linux machine, although I don't think the result is different on a windows (or other) machine. This is a simple question - C++ programs usually return a 32 bit int. If I return -1 , and then print the result from the terminal, the result is 255 . Why is this? I feel link this is something I should know, or should have noticed many years ago - I never

Prolog: how to convert string to integer?

微笑、不失礼 提交于 2019-12-23 10:40:05
问题 So as the title says - how do you convert a string into an integer? the idea is something like this: convert(String,Integer). examples: convert('1',1). convert('33',33). I'm using swi prolog 回答1: Use atom_number/2. E.g: atom_number('123', X). X = 123. 回答2: Assuming you really meant a string and not an atom, use number_codes . ?- number_codes(11, "11"). true. ?- number_codes(11, Str). Str = [49, 49]. % ASCII/UTF-8 ?- number_codes(N, "11"). N = 11. 回答3: Perhaps use of atom_codes(?Atom, ?String)

Getting a function to return two integers

前提是你 提交于 2019-12-23 10:15:49
问题 I am writing a function and I want it two return two integers as results. However, I cannot get it to do this. Could someone help me? Here is my best shot public static int calc (int s, int b, int c, int d, int g) { if (s==g) return s; else if (s+b==g) return s && b; else if (s + c==g) return s && c; else if (s+d==g) return s && d; else System.out.println("No Answer"); } 回答1: You could have the method return an array of int : public static int[] calc (int s, int b, int c, int d, int g) 回答2:

Is there anything in Rust to convert a binary string to an integer?

若如初见. 提交于 2019-12-23 09:32:43
问题 My binary resides as a string right now, I was hoping to format! it as an integer the same way I formatted my integer to binary: format!("{:b}", number) . I have a larger string of binary that I'm taking slices out of in a loop, so let's assume one of my slices is: let bin_idx: &str = "01110011001"; I want to format that binary into an integer: format!("{:i}", bin_idx); This gives a compiler error: error: unknown format trait `i` --> src/main.rs:3:21 | 3 | format!("{:i}", bin_idx); | ^^^^^^^

Using a regular expression to validate whether input has any non digits in it

安稳与你 提交于 2019-12-23 09:28:43
问题 function validInteger(theNumber){ var anyNonDigits = new RegExp('\D','g'); if(parseInt(theNumber)&&!anyNonDigits.test(theNumber)){ return true; }else{ return false; } } Above is a function I've written to validate some input. I want all positive integers. The problem I'm facing is with the RegExp object. This seems like it should be super simple, but for some reason it's not working. For example if I pass 'f5' I get true, but if I pass '5f' I get false. I'm also having problems when passing

Why does AtomicInteger implements Serializable

。_饼干妹妹 提交于 2019-12-23 09:25:39
问题 Accoriding to javadoc, public class AtomicInteger extends Number implements java.io.Serializable { // code for class } But, public abstract class Number implements java.io.Serializable { //code for class } If Number class already implements java.io.Serializable then why do AtomicInteger implements it again? Edit: Does Serializable being a marker interface makes any difference in this context? 回答1: Just to document it more clearly. Same situation with the abstract collection base classes.

How to convert DateTime to a number with a precision greater than days in T-SQL?

瘦欲@ 提交于 2019-12-23 07:29:17
问题 Both queries below translates to the same number SELECT CONVERT(bigint,CONVERT(datetime,'2009-06-15 15:00:00')) SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00') as bigint) Result 39978 39978 The generated number will be different only if the days are different. There is any way to convert the DateTime to a more precise number, as we do in .NET with the .Ticks property? I need at least a minute precision. 回答1: Well, I would do it like this: select datediff(minute,'1990-1-1',datetime) where

Java: Is it ok to set Integer = null?

纵饮孤独 提交于 2019-12-23 07:27:45
问题 I have a function that returns an id number if the argument exists in the database. If not, it returns null. Is this begging for a null pointer exception? Negative id numbers are not permitted, but I thought it would be clearer to have non-existent arguments returning null instead of an error code like -1. What do you think? private Integer tidOfTerm(String name) throws SQLException { String sql = "SELECT tid FROM term_data WHERE name = ?"; PreparedStatement prep = conn.prepareStatement(sql);

Integer Overflow - Why not [duplicate]

こ雲淡風輕ζ 提交于 2019-12-23 07:27:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned char a = 200; unsigned char b = 100; unsigned char c = (a + b) / 2; The output is 150 as logically expected, however shouldn't there be an integer overflow in the expression (a + b) ? Obviously there must be an integer promotion to deal with the overflow here, or something else is happening that I cannot see. I was wondering if someone

What is a C++ container with a “contains” operation?

放肆的年华 提交于 2019-12-23 07:02:10
问题 I want to use a structure in which I insert integers, and then can ask if (container.contains(3)) { /**/ } There has to be something like this. 回答1: You can use std::vector . std::vector<int> myVec; myVec.push_back(3); if (std::find(myVec.begin(), myVec.end(), 3) != myVec.end()) { // do your stuff } You can even make a little helper function: template <class T> bool contains(const std::vector<T> &vec, const T &value) { return std::find(vec.begin(), vec.end(), value) != vec.end(); } Here is