Search Results

Search found 1100 results on 44 pages for 'bitwise operators'.

Page 11/44 | < Previous Page | 7 8 9 10 11 12 13 14 15 16 17 18  | Next Page >

  • How can I access the sign bit of a number in C++?

    - by Keand64
    I want to be able to access the sign bit of a number in C++. My current code looks something like this: int sign bit = number >> 31; That appears to work, giving me 0 for positive numbers and -1 for negative numbers. However, I don't see how I get -1 for negative numbers: if 12 is 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1100 then -12 is 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0011 and shifting it 31 bits would make 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 which is 1, not -1, so why do I get -1 when I shift it?

    Read the article

  • Efficient bitshifting an array of int?

    - by nn
    Hi, To be on the same page, let's assume sizeof(int)=4 and sizeof(long)=8. Given an array of integers, what would be an efficient method to bitshift the array to either the left or right? I am contemplating an auxiliary variable such as a long, that will compute the bitshift for the first pair of elements (index 0 and 1) and set the first element (0). Continuing in this fashion the bitshift for elements (index 1 and 2) will be computer, and then index 1 will be set. I think this is actually a fairly efficient method, but there are drawbacks. I cannot bitshift greater than 32 bits. I think using multiple auxiliary variables would work, but I'm envisioning recursion somewhere along the line.

    Read the article

  • Setting last N bits in an array

    - by Martin
    I'm sure this is fairly simple, however I have a major mental block on it, so I need a little help here! I have an array of 5 integers, the array is already filled with some data. I want to set the last N bits of the array to be random noise. [int][int][int][int][int] set last 40 bits [unchanged][unchanged][unchanged][24 bits of old data followed 8 bits of randomness][all random] This is largely language agnostic, but I'm working in C# so bonus points for answers in C#

    Read the article

  • What is the trick in pAddress & ~(PAGE_SIZE - 1) to get the page's base address

    - by Dbger
    Following function is used to get the page's base address of an address which is inside this page: void* GetPageAddress(void* pAddress) { return (void*)((ULONG_PTR)pAddress & ~(PAGE_SIZE - 1)); } But I couldn't quite get it, what is the trick it plays here? Conclusion: Personally, I think Amardeep's explanation plus Alex B's example are best answers. As Alex B's answer has already been voted up, I would like to accept Amardeep's answer as the official one to highlight it! Thanks you all.

    Read the article

  • Convert bit vector (array of booleans) to an integer, and integer to bit vector, in Java.

    - by dreeves
    What's the best way to unstub the following functions? // Convert a bit-vector to an integer. int bitvec2int(boolean[] b) { [CODE HERE] } // Convert an integer x to an n-element bit-vector. boolean[] int2bitvec(int x, int n) { [CODE HERE] } Or is there a better way to do that sort of thing than passing boolean arrays around? This comes up in an Android app where we need an array of 20 booleans to persist and the easiest way to do that is to write an integer or string to the key-value store. I'll post the way we (Bee and I) wrote the above as an answer. Thanks!

    Read the article

  • Does this language feature already exist?

    - by Pindatjuh
    I'm currently developing a new language for programming in a continuous environment (compare it to electrical engineering), and I've got some ideas on a certain language construction. Let me explain the feature by explanation and then by definition: x = a U b; Where x is a variable and a and b are other variables (or static values). This works like a union between a and b; no duplicates and no specific order. with(x) { // regular 'with' usage; using the global interpretation of "x" x = 5; // will replace the original definition of "x = a U b;" } with(x = a) { // this code block is executed when the "x" variable // has the "a" variable assigned. All references in // this code-block to "x" are references to "a". So saying: x = 5; // would only change the variable "a". If the variable "a" // later on changes, x still equals to 5, in this fashion: // 'x = a U b U 5;' // '[currentscope] = 5;' // thus, 'a = 5;' } with(x = b) { // same but with "b" } with(x != a) { // here the "x" variable refers to any variable // but "a"; thus saying x = 5; // is equal to the rewriting of // 'x = a U b U 5;' // 'b = 5;' (since it was the scope of this block) } with(x = (a U b)) { // guaranteed that "x" is 'a U b'; interacting with "x" // will interact with both "a" and "b". x = 5; // makes both "a" and "b" equal to 5; also the "x" variable // is updated to contain: // 'x = a U b U 5;' // '[currentscope] = 5;' // 'a U b = 5;' // and thus: 'a = 5; b = 5;'. } // etc. In the above, all code-blocks are executed, but the "scope" changes in each block how x is interpreted. In the first block, x is guaranteed to be a: thus interacting with x inside that block will interact on a. The second and the third code-block are only equal in this situation (because not a: then there only remains b). The last block guarantees that x is at least a or b. Further more; U is not the "bitwise or operator", but I've called it the "and/or"-operator. Its definition is: "U" = "and" U "or" (On my blog, http://cplang.wordpress.com/2009/12/19/binop-and-or/, there is more (mathematical) background information on this operator. It's loosely based on sets. Using different syntax, changed it in this question.) Update: more examples. print = "Hello world!" U "How are you?"; // this will print // both values, but the // order doesn't matter. // 'userkey' is a variable containing a key. with(userkey = "a") { print = userkey; // will only print "a". } with(userkey = ("shift" U "a")) { // pressed both "shift" and the "a" key. print = userkey; // will "print" shift and "a", even // if the user also pressed "ctrl": // the interpretation of "userkey" is changed, // such that it only contains the matched cases. } with((userkey = "shift") U (userkey = "a")) { // same as if-statement above this one, showing the distributivity. } x = 5 U 6 U 7; y = x + x; // will be: // y = (5 U 6 U 7) + (5 U 6 U 7) // = 10 U 11 U 12 U 13 U 14 somewantedkey = "ctrl" U "alt" U "space" with(userkey = somewantedkey) { // must match all elements of "somewantedkey" // (distributed the Boolean equals operated) // thus only executed when all the defined keys are pressed } with(somewantedkey = userkey) { // matches only one of the provided "somewantedkey" // thus when only "space" is pressed, this block is executed. } Update2: more examples and some more context. with(x = (a U b)) { // this } // can be written as with((x = a) U (x = b)) { // this: changing the variable like x = 5; // will be rewritten as: // a = 5 and b = 5 } Some background information: I'm building a language which is "time-independent", like Java is "platform-independant". Everything stated in the language is "as is", and is continuously actively executed. This means; the programmer does not know in which order (unless explicitly stated using constructions) elements are, nor when statements are executed. The language is completely separated from the "time"-concept, i.e. it's continuously executed: with(a < 5) { a++; } // this is a loop-structure; // how and when it's executed isn't known however. with(a) { // everytime the "a" variable changes, this code-block is executed. b = 4; with(b < 3) { // runs only three times. } with(b > 0) { b = b - 1; // runs four times } } Update 3: After pondering on the type of this language feature; it closely resemblances Netbeans Platform's Lookup, where each "with"-statement a synchronized agent is, working on it's specific "filter" of objects. Instead of type-based, this is variable-based (fundamentally quite the same; just a different way of identifiying objects). I greatly thank all of you for providing me with very insightful information and links/hints to great topics I can research. Thanks. I do not know if this construction already exists, so that's my question: does this language feature already exist?

    Read the article

  • C Population Count of unsigned 64-bit integer with a maximum value of 15

    - by BitTwiddler1011
    I use a population count (hamming weight) function intensively in a windows c application and have to optimize it as much as possible in order to boost performance. More than half the cases where I use the function I only need to know the value to a maximum of 15. The software will run on a wide range of processors, both old and new. I already make use of the POPCNT instruction when Intel's SSE4.2 or AMD's SSE4a is present, but would like to optimize the software implementation (used as a fall back if no SSE4 is present) as much as possible. Currently I have the following software implementation of the function: inline int population_count64(unsigned __int64 w) { w -= (w 1) & 0x5555555555555555ULL; w = (w & 0x3333333333333333ULL) + ((w 2) & 0x3333333333333333ULL); w = (w + (w 4)) & 0x0f0f0f0f0f0f0f0fULL; return int(w * 0x0101010101010101ULL) 56; } So to summarize: (1) I would like to know if it is possible to optimize this for the case when I only want to know the value to a maximum of 15. (2) Is there a faster software implementation (for both Intel and AMD CPU's) than the function above?

    Read the article

  • What is the meaning of this pData[1+2*i]<<8|pData[2+2*i] C++ syntax?

    - by user543265
    what is the meqaning of pData[1+2*i]<<8|pData[2+2*i] where pData[ ] is the array containing BYTE data? I have the following function in the main function { .......... .... BYTE Receivebuff[2048]; .. ReceiveWavePacket(&Receivebuff[i], nNextStep); .... ... .. } Where Receivebuff is the array of type BYTE. ReceiveWavePacket(BYTE * pData, UINT nSize) { CString strTest; for(int i = 0 ; i < 60 ; i++) { strTest.Format("%d\n",(USHORT)(pData[1+2*i]<<8|pData[2+2*i])); m_edStatData.SetWindowTextA(strTest); } } I want to know the meaning of ",(USHORT)(pData[1+2*i]<<8|pData[2+2*i]). Can any body please help me?

    Read the article

  • Dojo Datagrid Filtering Issue

    - by Zoom Pat
    I am having hard time filtering a datagrid. Please help! This is how I draw a grid. var jsonStore = new dojo.data.ItemFileWriteStore({data:columnValues}); gridInfo = { store: jsonStore, queryOptions: {ignoreCase: true}, structure: layout }; grid = new dojox.grid.DataGrid(gridInfo, "gridNode"); grid.startup(); Now if i try something like this, it works fine and gives me the rows which has the column (AGE_FROM) value equal to 63. grid.filter({AGE_FROM:63}); but I need all kinds of filtering and not just 'equal to' So how do I try to obtain all the rows which have AGE_FROM 63, and < 63 and <= 63 and =63. because grid.filter({AGE_FROM:<63}); does not work Also One other way I was thingking was to use the following filteredStore = new dojox.jsonPath.query(filterData,"[?(@.AGE_FROM = 63]"); and then draw the grid with the filteredStore, but the above is not working for a != operator. Once I figure a good way to filter grid I need to see a way to filter out dates. I am trying to find a good example for filtering dataGrid but most of the examples are just filtering based on the 'equal to' criteria. Any help is highly appreciated.

    Read the article

  • What does the caret operator in Python do?

    - by Fry
    I ran across the caret operator in python today and trying it out, I got the following output: >>> 8^3 11 >>> 8^4 12 >>> 8^1 9 >>> 8^0 8 >>> 7^1 6 >>> 7^2 5 >>> 7^7 0 >>> 7^8 15 >>> 9^1 8 >>> 16^1 17 >>> 15^1 14 >>> It seems to be based on 8, so I'm guessing some sort of byte operation? I can't seem to find much about this searching sites other than it behaves oddly for floats, does anybody have a link to what this operator does or can you explain it here?

    Read the article

  • String concatenation: Final string value does not equal to the latest value

    - by Pan Pizza
    I have a simple question about string concatenation. Following is the code. I want to ask why s6 = "abcde" and not "akcde"? I have change the s2 value to "k". Public Class Form1 Public s1 As String = "a" Public s2 As String = "b" Public s3 As String = "c" Public s4 As String = "d" Public s5 As String = "e" Public s6 As String = "" Public s7 As String = "k" Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click s6 = s1 & s2 & s3 & s4 & s5 s2 = s7 MessageBox.Show(s6) 's6 = abcde End Sub End Class

    Read the article

  • operator for enums

    - by Veer
    Hi all, Just out of curiosity, asking this Like the expression one below a = (condition) ? x : y; // two outputs why can't we have an operator for enums? say, myValue = f ??? fnApple() : fnMango() : fnOrange(); // no. of outputs specified in the enum definition instead of switch statements (eventhough refractoring is possible) enum Fruit { apple, mango, orange }; Fruit f = Fruit.apple; Or is it some kind of useless operator?

    Read the article

  • Usage of ||, OR in PHP

    - by shin
    I have the following code which redirect pages depends on $path. ... $path = $this->uri->segment(3); $pathid = $this->uri->segment(4); if($path=='forsiden'){ redirect('','refresh'); }elseif($path =='contact'){ redirect('welcome/kontakt','refresh'); }elseif($path =='illustration'){ $this->_gallery($path,$pathid); }elseif($path =='webdesign'){ redirect('welcome/webdesign','refresh'); }elseif($path==('web_tjenester' || 'webdesigndetails' || 'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 'vpasp' || 'artportfolio')){ ... CODE A ... }else{ ... CODE B ... } I am not getting right results with $path==('web_tjenester' || 'webdesigndetails' || 'vismahjemmeside' || 'joomla' || 'vismanettbutikk' || 'vpasp' || 'artportfolio') contact, illustration, gallery and webdesign are redirected and working alright. However all other pages are added CODE A. I am expecting CODE A only when $path is web_tjenester', 'webdesigndetails', 'vismahjemmeside', 'joomla', 'vismanettbutikk', 'vpasp' or 'artportfolio'. Could anyone point out my mistake and correct me please? Thanks in advance. --UPDATE-- The following works, but is there any ways shorten the code? I am repeating ($path==..). elseif(($path=='web_tjenester') || ($path=='webdesigndetails') || ($path=='vismahjemmeside') || ($path=='joomla') || ($path=='vismanettbutikk') || ($path=='vpasp') || ($path=='artportfolio')){

    Read the article

  • Is there an "opposite" to the null coalescing operator? (…in any language?)

    - by Jay
    null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y;, where what follows the :: is evaluated only if what precedes it is not null. I see this as almost an opposite to null coalescence, kind of mixed in with a terse, inline null-check, but I'm [almost] certain that there is no such operator in C#. Are there other languages that have such an operator? If so, what is it called? (I know that I can write a method for it in C#; I use return NullOrValue.of(x, () => x.y);, but if you have anything better, I'd like to see that too.)

    Read the article

  • Null-coalescing operator and operator && in C#

    - by abatishchev
    Is it possible to use together any way operator ?? and operator && in next case: bool? Any { get { var any = this.ViewState["any"] as bool?; return any.HasValue ? any.Value && this.SomeBool : any; } } This means next: if any is null then this.Any.HasValue return false if any has value, then it returns value considering another boolean property, i.e. Any && SomeBool

    Read the article

  • Comparing Tuples in SQL

    - by Brad
    Is there any more convenient way to compare a tuple of data in T-SQL than doing something like this: SELECT TOP 100 A, B FROM MyTable WHERE (A > @A OR (A = @A AND B > @B)) ORDER BY A, B Basically I'm looking for rows with (A, B) (@A, @B) (the same ordering as I have in the order by clause). I have cases where I have 3 fields, but it just gets even uglier then.

    Read the article

  • What do you think about ??= operator in C#?

    - by TN
    Do you think that C# will support something like ??= operator? Instead of this: if (list == null) list = new List<int>(); It might be possible to write: list ??= new List<int>(); Now, I could use (but it seems to me not well readable): list = list ?? new List<int>();

    Read the article

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