bit-manipulation

What's the best way to convert from network bitcount to netmask?

喜欢而已 提交于 2019-11-29 14:08:21
问题 For example, if I have a network spec like 172.20.10.0/24, "24" is the bitcount. What's the best way to convert that to a netmask like 0xffffff00 ? 回答1: Why waste time with subtraction or ternary statements? int suffix = 24; int mask = 0xffffffff ^ 0xffffffff >> suffix; If you know your integer is exactly 32 bits long then you only need to type 0xffffffff once. int32_t mask = ~(0xffffffff >> suffix); Both compile to the exact same assembly code. 回答2: Assuming 32-bit mask and 32-bit int. int

Java how to parse uint8 in java?

≯℡__Kan透↙ 提交于 2019-11-29 14:07:20
问题 I have a uint8 (unsigned 8 bit integer) coming in from a UDP packet. Java only uses signed primitives. How do I parse this data structure correctly with java? 回答1: Simply read it as as a byte and then convert to an int. byte in = udppacket.getByte(0); // whatever goes here int uint8 = in & 0xFF; The bitmask is needed, because otherwise, values with bit 8 set to 1 will be converted to a negative int. Example: This: 10000000 Will result in: 11111111111111111111111110000000 So when you

Java and unsigned values

亡梦爱人 提交于 2019-11-29 14:03:38
问题 I'm parsing unsigned bits from a DatagramSocket. I have a total of 24bits (or 3 bytes) coming in - they are: 1 unsigned 8bit integer followed by a 16bit signed integer. But java never stores anything more than a signed byte into a byte/byte array? When java takes in these values, do you lose that last 8th bit? DatagramSocket serverSocket = new DatagramSocket(666); byte[] receiveData = new byte[3]; <--Now at this moment I lost my 8th bit System.out.println("Binary Server Listing on Port: "

Confusion with C ~ operator (bitwise Not) and comparing char variables

给你一囗甜甜゛ 提交于 2019-11-29 13:48:40
Using "ordinary C", I wish to compare two 8 bit bytes to determine if the second is the bitwise complement of the first. For example if Byte1 is binary 00001111 (15 in decimal) I want to test whether or not Byte2 is binary 11110000 (240 in decimal). I expected to do this using unsigned chars to represent the bytes, the C bitwise NOT operator "~" and a simple if( == ) test. Can anyone explain for me why the following code doesn't work (ie. I expect it to output "True" but it actually outputs "False"). unsigned char X = 15; unsigned char Y = 240; if( Y == ~X) printf("True"); else printf("False")

Bitwise operations on non numbers

无人久伴 提交于 2019-11-29 13:29:52
Somehow, JavaScript makes sense of the bitwise operations NaN ^ 1 , Infinity ^ 1 and even 'a' ^ 1 (all evaluate to 1 ). What are the rules governing bitwise operators on non numbers? Why do all the examples above evaluate to 1 ? According to the ES5 spec , when doing bitwise operations , all operands are converted to ToInt32 (which first calls ToNumber . If the value is NaN or Infinity , it's converted to 0 ). Thus: NaN ^ 1 => 0 XOR 1 => 1 Oleg V. Volkov ECMA-262 defines in 11.10 that arguments of binary bitwise operators are converted with ToInt32. And 9.5 that explains ToInt32 says in its

What's the function of the ~ bitwise operator (Tilde) [duplicate]

梦想的初衷 提交于 2019-11-29 13:23:38
Possible Duplicate: What does this ~ operator mean here? Bit not operation in PHP(or any other language probably) Can someone explain me the ~ operator in PHP? I know it's a NOT-operator , but why does PHP convert following statement to the negative value of the variable minus one? $a = 1; echo ~$a // echo -2 $a = 2; echo ~$a // echo -3 $a = 3; echo ~$a // echo -4 This is called the two's complement arithmetic . You can read about it in more detail here . The operator ~ is a binary negation operator (as opposed to boolean negation), and being that, it inverses all the bits of its operand. The

Performing bit division without arithmetic operators [closed]

*爱你&永不变心* 提交于 2019-11-29 13:05:20
I am trying to complete an assignment that requires me to write three functions for binary arithmetic. badd() was provided for me, so I used it to help write the bsub() and bmult() functions. I am having trouble understanding how I should perform the bdiv() function, however. I know I need to iterate through the bits using a right shift and my bsubb() function, but I don't know how to implement it. Below are the functions that I have written so far. Let me know if you notice any mistakes that I made in writing them(meaning bsub() and bmult()). Thanks. /** This function adds the two arguments

Why the bit operation i & (-i) equals to rightmost bit?

天大地大妈咪最大 提交于 2019-11-29 12:51:57
I learned Fenwick Tree algorithm and there was written "i & (-i) equals to rightmost bit". For example, 3 & (-3) = 1, 48 & (-48) = 16. . I tested the result for i <= 64 , and all values satisfied the condition. But I don't know why the condition satisfies (proof) for all positive integer i. Please tell me how to prove. EDIT: You can assume that i is 32-bit integer (But 16-bit is ok). If so, the range of value i is 1 <= i <= 2^31-1 . Say you've got a two's complement binary number i : 0b1101001010000000 and you want to find -i . Well, -i is the number such that i + (-i) == 0 . So what number

Why this code for addition(using bitwise operation) works in java

最后都变了- 提交于 2019-11-29 12:43:48
问题 public int add(int a, int b){ while (b != 0){ int carry = (a & b) ; a = a ^ b; b = carry << 1; } return a; } This is the code to calculate sum of two integers using bitwise operation. If i calculate manually/programmatically, i see it works for every integer. But i am not able to figure out any relation between intermediate value of a and carry . and why carry is multiplied by 2 to assign to b ? PS: I found the one answer here Bitwise Multiply and Add in Java but this is for multiplication

javascript bitwise operator question

谁说我不能喝 提交于 2019-11-29 12:38:39
In Javascript when I do this var num = 1; ~ num == -2 why does ~num not equal 0 in binary 1 is stored as 1 ... thus not 1 should be 0 or it is stored like 0001 thus not 0001 would be 1110 I think I am missing something... can someone clear this up Look up Two's complement for signed binary numbers Lets assume that a javascript Number is 8 bits wide (which its not): then 1 = 0000 0001b and ~1 = 1111 1110b Which is the binary representation of -2 0000 0010b = 2 0000 0001b = 1 0000 0000b = 0 1111 1111b = -1 1111 1110b = -2 ~ toggles the bits of the operand so 00000001 becomes 11111110 which is -2