bitwise

Do .net FlagsAttribute enums need to be manually valued?

To allow for different formatting options on a method that displays a news story, I've created an enumeration that can be passed in to specify how it gets displayed. [Flags] private enum NewsStyle { Thumbnail = 0, Date = 1, Text = 2, Link = 4, All = 8 } string FormatNews( DataRow news, NewsStyle style ) { String...

bit pack Int16 into a Ushort VB.net

I have to pack and unpack a 16bit Int from/into a Ushort in VB.net This is how I thought I could do it (doesn't work, gives me overflow exception) 'Pack Int16 into ushort ' Dim usPacked = CType(Data, UShort) 'unpack Int16 from ushort ' Dim unpacked = CType(data,Int16) Thanks! ...

Branchless code that maps zero, negative, and positive to 0, 1, 2

Write a branchless function that returns 0, 1, or 2 if the difference between two signed integers is zero, negative, or positive. Here's a version with branching: int Compare(int x, int y) { int diff = x - y; if (diff == 0) return 0; else if (diff < 0) return 1; else return 2; } Here's a versio...

Is it possible to create a generic bitwise enumeration 'IsOptionSet()' method?

The below code makes it easy to pass in a set HtmlParserOptions and then check against a single option to see if it's been selected. [Flags] public enum HtmlParserOptions { NotifyOpeningTags = 1, NotifyClosingTags = 2, NotifyText = 4, NotifyEmptyText = 8 } private bool IsOptionSet(HtmlParserOptions options, HtmlParserOp...

Sql Server 2000: Return "true" or "false" based on any 1 of 25 columns being "true"

Hi, I have to create a query that checks across several different columns, and if any of them have a 1, I want to return true. Ideal output would be along the lines of: ID:55 Name:John Doe IsDealerType1:True IsDealerType2:True IsDealerType3:False IsDealerType4:False IsDealerType5:True The problem is, instead of those 5 dealer col...

Python bitwise operations confusion

I came up with this "magic string" to meet the ID3 tagging specification: The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01. >>> hex_val = 0xFFFFFFFF >>> str...

Return zero for negative integers

A friend just throw some code similar to following C# code: int i = ...; return i < 0 ? 0 : i; That made me think. There's any "different" way to return zero for negative integers, or current positive value? More specifically I'm looking for bitwise operations, if possible. BTW, I'm aware of Math.Max(0, i); ...

Difference between ^ Operator in JS and Python

I need to port some JS code which involves Math.random()*2147483648)^(new Date).getTime(). While it looks like for smaller numbers, the python function and the JS function are equivalent in function, but with large numbers like this, the values end up entirely different. Python: 2147483647^1257628307380 =1257075044427 Javascr...

Simulate a 128-bit unsigned integer in SQL and C# using a 64-bit signed value?

Take this scenario: You have a few flag enumerations in C# tied to (and in fact generated from) Enum-ish tables in SQL Server. Say you are a distributor, and you allow your resellers to specify what US states they ship to. Being a brilliant and elegant software engineer, you implemented these as a bitwise-combinable flag value to save st...

Bitwise Operation and Usage

x = 1 # 0001 x << 2 # Shift left 2 bits: 0100 # Result: 4 x | 2 # Bitwise OR: 0011 # Result: 3 x & 1 # Bitwise AND: 0001 # Result: 1 I can understand the arithmetic operators in Python (and other langs), but I never understood 'bitwise' operators quite well. In the above example (from a Python book), I und...

store a string in an int

Hi, i try to store a string into an integer as follows: i read the characters of the string and every 4 characters i do this: val = (int) ch << 24 | (int) ch << 16 | (int) ch << 8 | (int) ch; Then i put the integer value in an array of integer that is called memory ( => int memory[16]). I would like to do it in an automatic way for ...

Javascript bitwise problem

Hi, I have a problem while using bitwise in javascript. I don't know if I'm going about this the wrong way. But here goes. I have 4 main categories. With id 1,2,4,8. A item in my object has a property with the total of the categories it's a member of. ie. { item: { name: 'lorem', c: 7 }} //member of category 1,2,4 I have variable ...

Writing files in bit form to a file in C

i am implementing the huffman algorithm in C. i have got the basic functionality down upto the point where the binary codewords are obtained. so for example, abcd will be 100011000 or something similar. now the question is how do you write this code in binary form in the compressed file. i mean if i write it normally each 1 and 0 will be...

Why do a lot of languages lack a logical XOR operator?

Off the top of my head, I cannot think of a single language I've used that had a logical exclusive or operator, but all have logical and bitwise and and or operators. Looking around, the only reason to this that I could find was that exclusive or cannot be short circuited, so a logical version would be useless, which I really can't see ...

Bit operations in C

So if I have an integer that is 32 bits. The first 28 bits (from left) are to store the size of a memory chunk, the next two are 0s and the last two are: to store the if it is the last node and then to store if it is used or not (respectively). What I am trying to do is to know how to turn the flag on and off on the isLast operation ...

Porting C code; need help with bitwise operation and pointer syntax

I have some C code that I'd like to port to java. I haven't done much C coding, but I was able to follow along up until this one function. If anyone could help me understand what is going on, it would be greatly appreciated. int reverse_integer(int input) { int output = 0, i; for ( i=0, i<sizeof(int); i++ ) { output =...

Unsigned Integer in Javascript

I'm working on a page that processes IP address information, but it's choking on the fact that integers are signed. I am using bitwise operators to speed it up, but the 64th bit (signed/unsigned flag) is messing it up. Is there any way to force a number to be unsigned in Javascript? It seems to work fine, until subnet is greater than 30...

Good tutorials on Bitwise operations in Java

Can anyone please point me to good online tutorials on Bitwise Operations (preferably in Java)? Before anyone write LMGTFY, I've already Googled it but couldn't find one with good examples. Java's own tutorial is also not that extensive and doesn't clearly explain what is going under the hood. I'm a visual learner; so, if there are any r...

T-Sql: check if IP matches netmask

My SQL-Server db stores IP netmasks as binary. I need a way to match these with a given IP for example: is 192.168.21.5 part of a netmask which is stored in the db? the binary representation of 192.168.21.5: 11000000.10101000.00010101.00000101 (without the dots) Netmask stored in db is: binary(4) and a tinyint field 11000000.1010100...

How do you randomly zero a bit in an integer?

Updated with newer answer and better test Let's say I have the number 382 which is 101111110. How could I randomly turn a bit which is not 0 to 0? The why; Since people ask me why, I simply need to do this, removing a bit from an integer. based on the answer here is the result(working one) I ran this using System; using System.C...