Search Results

Search found 977 results on 40 pages for 'operators'.

Page 9/40 | < Previous Page | 5 6 7 8 9 10 11 12 13 14 15 16  | Next Page >

  • The ternary (conditional) operator in C

    - by Bongali Babu
    What is the need for the conditional operator? Functionally it is redundant, since it implements an if-else construct. If the conditional operator is more efficient than the equivalent if-else assignment, why can't if-else be interpreted more efficiently by the compiler?

    Read the article

  • bit manipulation in java

    - by sarav
    I have a fragment of bytes in a byte[]. The total size of the array is 4 and I want to convert this into a positive long number. For example if the byte array is having four bytes 101, 110, 10, 1 then i want to get the long number represented by binary sequence 00000000 00000000 00000000 00000000 00000101 00000110 00000010 00000001 which equals 84279809 What is the efficient way to do that in Java?

    Read the article

  • null coalescing operator for javascript?

    - by Daniel Schaffer
    I assumed this question has already been asked here, but I couldn't find any, so here it goes: Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? "Cookies!"; The best approximation I can figure out for Javascript is using the conditional operator: var someString = null; var whatIWant = someString ? someString : 'Cookies!'; Which is sorta icky IMHO. Can I do better?

    Read the article

  • C question: Padding bits in unsigned integers and bitwise operations (C89)

    - by Anonymous Question Guy
    I have a lot of code that performs bitwise operations on unsigned integers. I wrote my code with the assumption that those operations were on integers of fixed width without any padding bits. For example an array of 32 bit unsigned integers of which all 32 bits available for each integer. I'm looking to make my code more portable and I'm focused on making sure I'm C89 compliant (in this case). One of the issues that I've come across is possible padded integers. Take this extreme example, taken from the GMP manual: However on Cray vector systems it may be noted that short and int are always stored in 8 bytes (and with sizeof indicating that) but use only 32 or 46 bits. The nails feature can account for this, by passing for instance 8*sizeof(int)-INT_BIT. I've also read about this type of padding in other places. I actually read of a post on SO last night (forgive me, I don't have the link and I'm going to cite something similar from memory) where if you have, say, a double with 60 usable bits the other 4 could be used for padding and those padding bits could serve some internal purpose so they cannot be modified. So let's say for example my code is compiled on a platform where an unsigned int type is sized at 4 bytes, each byte being 8 bits, however the most significant 2 bits are padding bits. Would UINT_MAX in that case be 0x3FFFFFFF (1073741823) ? #include <stdio.h> #include <stdlib.h> /* padding bits represented by underscores */ int main( int argc, char **argv ) { unsigned int a = 0x2AAAAAAA; /* __101010101010101010101010101010 */ unsigned int b = 0x15555555; /* __010101010101010101010101010101 */ unsigned int c = a ^ b; /* ?? __111111111111111111111111111111 */ unsigned int d = c << 5; /* ?? __111111111111111111111111100000 */ unsigned int e = d >> 5; /* ?? __000001111111111111111111111111 */ printf( "a: %X\nb: %X\nc: %X\nd: %X\ne: %X\n", a, b, c, d, e ); return 0; } is it safe to XOR two integers with padding bits? wouldn't I XOR whatever the padding bits are? I can't find this behavior covered in C89. furthermore is the c var guaranteed to be 0x3FFFFFFF or if for example the two padding bits were both on in a or b would c be 0xFFFFFFFF ? same question with d and e. am i manipulating the padding bits by shifting? I would expect to see this below, assuming 32 bits with the 2 most significant bits used for padding, but I want to know if something like this is guaranteed: a: 2AAAAAAA b: 15555555 c: 3FFFFFFF d: 3FFFFFE0 e: 01FFFFFF Also are padding bits always the most significant bits or could they be the least significant bits? Thanks guys EDIT 12/19/2010 5PM EST: Christoph has answered my question. Thanks! I had also asked (above) whether padding bits are always the most significant bits. This is cited in the rationale for the C99 standard, and the answer is no. I am playing it safe and assuming the same for C89. Here is specifically what the C99 rationale says for §6.2.6.2 (Representation of Integer Types): Padding bits are user-accessible in an unsigned integer type. For example, suppose a machine uses a pair of 16-bit shorts (each with its own sign bit) to make up a 32-bit int and the sign bit of the lower short is ignored when used in this 32-bit int. Then, as a 32-bit signed int, there is a padding bit (in the middle of the 32 bits) that is ignored in determining the value of the 32-bit signed int. But, if this 32-bit item is treated as a 32-bit unsigned int, then that padding bit is visible to the user’s program. The C committee was told that there is a machine that works this way, and that is one reason that padding bits were added to C99. Footnotes 44 and 45 mention that parity bits might be padding bits. The committee does not know of any machines with user-accessible parity bits within an integer. Therefore, the committee is not aware of any machines that treat parity bits as padding bits. EDIT 12/28/2010 3PM EST: I found an interesting discussion on comp.lang.c from a few months ago. Bitwise Operator Effects on Padding Bits (VelocityReviews reader) Bitwise Operator Effects on Padding Bits (Google Groups alternate link) One point made by Dietmar which I found interesting: Let's note that padding bits are not necessary for the existence of trap representations; combinations of value bits which do not represent a value of the object type would also do.

    Read the article

  • overload Equals, is this wrong?

    - by Zka
    Reading some piece of code and I keep seeing this : public override bool Equals (object obj) { if (obj == null || this.GetType ().Equals (obj.GetType())) return false; //compare code... } Shouldn't it be like this (note the !): public override bool Equals (object obj) { if (obj == null || !this.GetType ().Equals (obj.GetType())) return false; //compare code... } Or does the equals perform differently in this case?

    Read the article

  • Boolean 'NOT' in T-SQL not working on 'bit' datatype?

    - by Joannes Vermorel
    Trying to perform a single boolean NOT operation, it appears that under MS SQL Server 2005, the following block does not work DECLARE @MyBoolean bit; SET @MyBoolean = 0; SET @MyBoolean = NOT @MyBoolean; SELECT @MyBoolean; Instead, I am getting more successful with DECLARE @MyBoolean bit; SET @MyBoolean = 0; SET @MyBoolean = 1 - @MyBoolean; SELECT @MyBoolean; Yet, this looks a bit a twisted way to express something as simple as a negation. Am I missing something?

    Read the article

  • Can't operator == be applied to generic types in C#?

    - by Hosam Aly
    According to the documentation of the == operator in MSDN, For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings. User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described above for both predefined and user-defined reference types. So why does this code snippet fail to compile? void Compare<T>(T x, T y) { return x == y; } I get the error Operator '==' cannot be applied to operands of type 'T' and 'T'. I wonder why, since as far as I understand the == operator is predefined for all types? Edit: Thanks everybody. I didn't notice at first that the statement was about reference types only. I also thought that bit-by-bit comparison is provided for all value types, which I now know is not correct. But, in case I'm using a reference type, would the the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one? Edit 2: Through trial and error, we learned that the == operator will use the predefined reference comparison when using an unrestricted generic type. Actually, the compiler will use the best method it can find for the restricted type argument, but will look no further. For example, the code below will always print true, even when Test.test<B>(new B(), new B()) is called: class A { public static bool operator==(A x, A y) { return true; } } class B : A { public static bool operator==(B x, B y) { return false; } } class Test { void test<T>(T a, T b) where T : A { Console.WriteLine(a == b); } }

    Read the article

  • SQL Logic Operator Precedence: And and Or

    - by nc
    Are the two statements below equivalent? SELECT [...] FROM [...] WHERE some_col in (1,2,3,4,5) AND some_other_expr and SELECT [...] FROM [...] WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr Is there some sort of truth table I could use to verify this? Thanks.

    Read the article

  • How to change default conjunction with Lucene MultiFieldQueryParser

    - by Luke H
    I have some code using Lucene that leaves the default conjunction operator as OR, and I want to change it to AND. Some of the code just uses a plain QueryParser, and that's fine - I can just call setDefaultOperator on those instances. Unfortunately, in one place the code uses a MultiFieldQueryParser, and calls the static "parse" method (taking String, String[], BooleanClause.Occur[], Analyzer), so it seems that setDefaultOperator can't help, because it's an instance method. Is there a way to keep using the same parser but have the default conjunction changed?

    Read the article

  • "<" operator error

    - by Nona Urbiz
    Why is the ( i < UniqueWords.Count ) expression valid in the for loop, but returns "CS0019 Operator '<' cannot be applied to operands of type 'int' and 'method group'" error when placed in my if? They are both string arrays, previously declared. for (int i = 0;i<UniqueWords.Count;i++){ Occurrences[i] = Words.Where(x => x.Equals(UniqueWords[i])).Count(); Keywords[i] = UniqueWords[i]; if (i<UniqueURLs.Count) {rURLs[i] = UniqueURLs[i];} } EDITED to add declarations: List<string> Words = new List<string>(); List<string> URLs = new List<string>(); //elements added like so. . . . Words.Add (referringWords); //these are strings URLs.Add (referringURL); UniqueWords = Words.Distinct().ToList(); UniqueURLs = URLs.Distinct().ToList(); SOLVED. thank you, parentheses were needed for method .Count() I still do not fully understand why they are not always necessary. Jon Skeet, thanks, I guess I don't understand what exactly the declarations are either then? You wanted the actual values assigned? They are pulled from an external source, but are strings. I get it! Thanks. (the ()'s at least.)

    Read the article

  • Declaring functors for comparison ??

    - by Mr.Gando
    Hello, I have seen other people questions but found none that applied to what I'm trying to achieve here. I'm trying to sort Entities via my EntityManager class using std::sort and a std::vector<Entity *> /*Entity.h*/ class Entity { public: float x,y; }; struct compareByX{ bool operator()(const GameEntity &a, const GameEntity &b) { return (a.x < b.x); } }; /*Class EntityManager that uses Entitiy*/ typedef std::vector<Entity *> ENTITY_VECTOR; //Entity reference vector class EntityManager: public Entity { private: ENTITY_VECTOR managedEntities; public: void sortEntitiesX(); }; void EntityManager::sortEntitiesX() { /*perform sorting of the entitiesList by their X value*/ compareByX comparer; std::sort(entityList.begin(), entityList.end(), comparer); } I'm getting a dozen of errors like : error: no match for call to '(compareByX) (GameEntity* const&, GameEntity* const&)' : note: candidates are: bool compareByX::operator()(const GameEntity&, const GameEntity&) I'm not sure but ENTITY_VECTOR is std::vector<Entity *> , and I don't know if that could be the problem when using the compareByX functor ? I'm pretty new to C++, so any kind of help is welcome.

    Read the article

  • trying to make a simple grid-class, non-lvalue in assignment

    - by Tyrfing
    I'm implementing a simple C++ grid class. One Function it should support is accessed through round brackets, so that I can access the elements by writing mygrid(0,0). I overloaded the () operator and i am getting the error message: "non-lvalue in assignment". what I want to be able to do: //main cGrid<cA*> grid(5, 5); grid(0,0) = new cA(); excerpt of my implementation of the grid class: template class cGrid { private: T* data; int mWidth; int mHeight; public: cGrid(int width, int height) : mWidth(width), mHeight(height) { data = new T[width*height]; } ~cGrid() { delete data; } T operator ()(int x, int y) { if (x >= 0 && x <= mWidth) { if (y >= 0 && y <= mHeight) { return data[x + y * mWidth]; } } } const T &operator ()(int x, int y) const { if (x >= 0 && x <= mWidth) { if (y >= 0 && y <= mHeight) { return data[x + y * mWidth]; } } } The rest of the code deals with the implementation of an iterator and should not be releveant.

    Read the article

  • Allow member to be const while still supporting operator= on the class

    - by LeopardSkinPillBoxHat
    I have several members in my class which are const and can therefore only be initialised via the initialiser list like so: class MyItemT { public: MyItemT(const MyPacketT& aMyPacket, const MyInfoT& aMyInfo) : mMyPacket(aMyPacket), mMyInfo(aMyInfo) { } private: const MyPacketT mMyPacket; const MyInfoT mMyInfo; }; My class can be used in some of our internally defined container classes (e.g. vectors), and these containers require that operator= is defined in the class. Of course, my operator= needs to do something like this: MyItemT& MyItemT::operator=(const MyItemT& other) { mMyPacket = other.mPacket; mMyInfo = other.mMyInfo; return *this; } which of course doesn't work because mMyPacket and mMyInfo are const members. Other than making these members non-const (which I don't want to do), any ideas about how I could fix this?

    Read the article

  • Using string constants in implicit conversion

    - by kornelijepetak
    Consider the following code: public class TextType { public TextType(String text) { underlyingString = text; } public static implicit operator String(TextType text) { return text.underlyingString; } private String underlyingString; } TextType text = new TextType("Something"); String str = text; // This is OK. But I want to be able do the following, if possible. TextType textFromStringConstant = "SomeOtherText"; I can't extend the String class with the TextType implicit operator overload, but is there any way to assign a literal string to another class (which is handled by a method or something)? String is a reference type so when they developed C# they obviously had to use some way to get a string literal to the class. I just hope it's not hardcoded into the language.

    Read the article

  • F# passing an operator with arguments to a function

    - by dan
    Can you pass in an operation like "divide by 2" or "subtract 1" using just a partially applied operator, where "add 1" looks like this: List.map ((+) 1) [1..5];; //equals [2..6] // instead of having to write: List.map (fun x-> x+1) [1..5] What's happening is 1 is being applied to (+) as it's first argument, and the list item is being applied as the second argument. For addition and multiplication, this argument ordering doesn't matter. Suppose I want to subtract 1 from every element (this will probably be a common beginners mistake): List.map ((-) 1) [1..5];; //equals [0 .. -4], the opposite of what we wanted 1 is applied to the (-) as its first argument, so instead of (list_item - 1), I get (1 - list_item). I can rewrite it as adding negative one instead of subtracting positive one: List.map ((+) -1) [1..5];; List.map (fun x -> x-1) [1..5];; // this works too I'm looking for a more expressive way to write it, something like ((-) _ 1), where _ denotes a placeholder, like in the Arc language. This would cause 1 to be the second argument to -, so in List.map, it would evaluate to list_item - 1. So if you wanted to map divide by 2 to the list, you could write: List.map ((/) _ 2) [2;4;6] //not real syntax, but would equal [1;2;3] List.map (fun x -> x/2) [2;4;6] //real syntax equivalent of the above Can this be done or do I have to use (fun x -> x/2)? It seems that the closest we can get to the placeholder syntax is to use a lambda with a named argument.

    Read the article

  • JSF Deferred EL conditional syntax problem

    - by Mark Lewis
    Hello I can't find any resources which can answer why I'm getting an error with this: oncomplete="#{MyBacking.oError ? #{rich:component('oErrorPanel')}.show() : return false;}" in a richfaces a4j:commandButton. oError is referring to a method in my bean called isOError. I'm getting the error SEVERE: Servlet.service() for servlet Faces Servlet threw exception org.apache.el.parser.ParseException: Encountered " "?" "? "" at line 1, column 30. Was expecting one of: "}" ... "." ... ... I want to say 'if a method returns true, show modal panel A otherwise false'. Any help much appreciated.

    Read the article

  • What does !! (double exclamation point) mean?

    - by molecules
    In the code below, from a blog post by Alias, I noticed the use of the double exclamation point !!. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for '!!' at perlsyn). package Foo; use vars qw{$DEBUG}; BEGIN { $DEBUG = 0 unless defined $DEBUG; } use constant DEBUG => !! $DEBUG; sub foo { debug('In sub foo') if DEBUG; ... } UPDATE Thanks for all of your answers. Here is something else I just found that is related The List Squash Operator x!!

    Read the article

  • assign operator to variable in python?

    - by abhilashm86
    Usual method of applying mathematics to variables is a * b Is it able to calculate and manipulate two operands like this? a = input('enter a value') b = input('enter a value') op = raw_input('enter a operand') then how do i connect op and two variables a and b?? i know i can compare op to +, -, %, $ and then assign and compute.... but can i do something like a op b , how to tell compiler that op is an operator?? any tweaks possible?

    Read the article

  • C#: Shift left assignment operator behavior

    - by Austin Salonen
    I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes left == right and no shift occurs current <<= (32 + left - right); //this works current <<= (32 - right); current <<= left; It appears for any value = 32, only the value % 32 is shifted. Is there some "optimization" occurring in the framework?

    Read the article

< Previous Page | 5 6 7 8 9 10 11 12 13 14 15 16  | Next Page >