integer

Increment integer in NSUserDefaults

谁都会走 提交于 2019-12-21 18:57:50
问题 Is there a more elegant way to increment a counter stored in user defaults? let defaults = NSUserDefaults.standardUserDefaults() defaults.setInteger(defaults.integerForKey("counter")+1, forKey: "counter") 回答1: No but if you do this a lot it might make a nice extension, something like this. extension NSUserDefaults { class func incrementIntegerForKey(key:String) { let defaults = standardUserDefaults() let int = defaults.integerForKey(key) defaults.setInteger(int+1, forKey:key) } } Usage like

How do I do modulus in C++?

情到浓时终转凉″ 提交于 2019-12-21 17:54:23
问题 How do I perform a mod operation between two integers in C++? 回答1: Like this: x=y%z 回答2: In c++, use % operator More Help 回答3: As the other answers have stated, you can use the C++ % operator. But be aware that there's a wrinkle no one has mentioned yet: in the expression a % b , what if a is negative? Should the result of this operation be positive or negative? The C++ standard leaves this up to the implementation. So if you want to handle negative inputs portably, you should probably do

how to pass sql parameter as null value in integer datatype variable?

我的未来我决定 提交于 2019-12-21 09:30:11
问题 how to pass sql parameter as null value in to integer data type variable ? StockBO sBO = new StockBO(); sBO.Mode = 2; if (ddcmpanyname.SelectedIndex != 0) { sBO.Client_id = Convert.ToInt16(ddcmpanyname.SelectedValue); } else { sBO.Client_id = Convert.ToInt16(DBNull.Value);//Object cannot be cast from DBNull to other types. sBO.Client_id =DBNull.Value;//Cannot implicitly convert type } ds=_StockController.StockGet(sBO); i changed the code like this it gives error like i my comment check else

How does this bit manipulation work in Java?

拥有回忆 提交于 2019-12-21 09:25:06
问题 I was looking into how Java counts bits set of an int. I had in my mind something simple like this (which I think is correct): public static int bitCount(int number){ final int MASK = 0x1; int count = 0; for(int i = 0; i < 32; i++){ if(((number >>> i) & MASK) == MASK){ count++; } } return count; } Instead I found a method that I absolutely have no idea what is doing (seems like magic to me): i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4))

String to Integer Smalltalk

こ雲淡風輕ζ 提交于 2019-12-21 08:01:27
问题 Pretty simple question I need to get an integer from the user and I only know how to get a string from them. So if there is a way to get an integer from the user or to convert the string to an integer please let me know. 回答1: Found it: '12345' asNumber. 回答2: If you are using Squeak or Pharo, learn to use the "Message Finder". Left click on the background, click Tools > Message Finder. Click on the top left pane. Type: '12345'. 12345. Hit Control-S and you have your answer. Specifically, you

String to Integer Smalltalk

北战南征 提交于 2019-12-21 08:01:07
问题 Pretty simple question I need to get an integer from the user and I only know how to get a string from them. So if there is a way to get an integer from the user or to convert the string to an integer please let me know. 回答1: Found it: '12345' asNumber. 回答2: If you are using Squeak or Pharo, learn to use the "Message Finder". Left click on the background, click Tools > Message Finder. Click on the top left pane. Type: '12345'. 12345. Hit Control-S and you have your answer. Specifically, you

Converting a String with spaces to an Integer in java

回眸只為那壹抹淺笑 提交于 2019-12-21 07:40:11
问题 I'm trying to convert a String variable into an integer, only the String looks like this (for example): String string = " 12"; And so the String has a space in front of it. Now the String variable is being read from a .txt file which is why I'm having the issue of having the space in front of it. I need to convert the variable into an int, but when I try: int integer = Integer.parseInt(string); to pass the String into an integer, it compiles but I get an error when trying to run the program,

Why do I get an error when directly comparing two enums?

喜欢而已 提交于 2019-12-21 07:29:52
问题 I have some code I'm porting to a new platform, and it started giving me an error about comparing two enumerators from two different enumerator-lists. I'm confused why it's giving me an error about this. The enumeration specificers section of the C spec (6.7.2.2) states: The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. 127) An enumerator with = defines its enumeration constant as the value of the constant expression

Can someone explain how to append an element to an array in C programming?

被刻印的时光 ゝ 提交于 2019-12-21 07:25:52
问题 If I want to append a number to an array initialized to int, how can I do that? int arr[10] = {0, 5, 3, 64}; arr[] += 5; //Is this it?, it's not working for me... I want {0,5, 3, 64, 5} in the end. I'm used to Python, and in Python there is a function called list.append that appends an element to the list automatically for you. Does such function exist in C? 回答1: int arr[10] = {0, 5, 3, 64}; arr[4] = 5; EDIT: So I was asked to explain what's happening when you do: int arr[10] = {0, 5, 3, 64};

What does i+=(i&-i) do? Is it portable?

心已入冬 提交于 2019-12-21 07:04:47
问题 Let i be a signed integer type. Consider i += (i&-i); i -= (i&-i); where initially i>0 . What do these do? Is there an equivalent code using arithmetic only? Is this dependent on a specific bit representation of negative integers? Source: setter's code of an online coding puzzle (w/o any explanation/comments). 回答1: If i has unsigned type, the expressions are completely portable and well-defined. If i has signed type, it's not portable, since & is defined in terms of representations but unary