bits

How to change 4 bits in unsigned char?

梦想的初衷 提交于 2019-12-06 04:22:48
问题 unsigned char *adata = (unsigned char*)malloc(500*sizeof(unsigned char)); unsigned char *single_char = adata+100; How do I change first four bits in single_char to represent values between 1..10 (int)? The question came from TCP header structure: Data Offset: 4 bits The number of 32 bit words in the TCP Header. This indicates where the data begins. The TCP header (even one including options) is an integral number of 32 bits long. Usually it has value of 4..5, the char value is like 0xA0. 回答1:

What is size of my Bitset?

血红的双手。 提交于 2019-12-05 21:39:26
问题 I want to store System.currentTimeInMillis in memory with minimum space possible. because I have to store millions of them in memory. I converted it to binaryString which gave me 41 bits Here is my program public class BitSetSize { public static void main(final String[] args) { final long currentTimeMillis = System.currentTimeMillis(); final String currentTimeToBinaryString = Long.toBinaryString(currentTimeMillis); System.out.println("Size in bits: " + currentTimeToBinaryString.length());

Number of bits to represent a number

淺唱寂寞╮ 提交于 2019-12-05 18:55:53
问题 I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries. Method 1: function bitSize(num) { return Math.floor( Math.log(num) / Math.log(2) ) + 1; } Pass: bitSize( Math.pow(2, 16) -1 ) = 16 Pass: bitSize( Math.pow(2, 16) ) = 17 Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 Pass: bitSize( Math.pow(2, 48) ) = 49 Method 2: function

Swap bits in c++ for a double

时光总嘲笑我的痴心妄想 提交于 2019-12-05 17:37:40
Im trying to change from big endian to little endian on a double. One way to go is to use double val, tmp = 5.55; ((unsigned int *)&val)[0] = ntohl(((unsigned int *)&tmp)[1]); ((unsigned int *)&val)[1] = ntohl(((unsigned int *)&tmp)[0]); But then I get a warning: "dereferencing type-punned pointer will break strict-aliasing rules" and I dont want to turn this warning off. Another way to go is: #define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x >> 32)) ) ) val = (double)bswap_64(unsigned long long(tmp)); //or val = (double)ntohll(unsigned long

printing the binary representation of a number

狂风中的少年 提交于 2019-12-05 16:23:24
What is wrong with the below code which prints the binary representation of a number? int a = 65; for (int i = 0; i < 8; i++) { cout << ((a >> i) & 1); } You're starting at the least significant bit in the number and printing it first. However, whatever you print first is the most significant digit in the typical binary representation. 65 is 01000001 so this is how your loop iterates 01000001 ^ Output: 1 01000001 ^ Output: 10 01000001 ^ Output: 100 ... 01000001 ^ Output: 10000010 Thus the printed output is in reverse. The simplest fix is to change the order of the loop. for (int i = 7; i >= 0;

Converting bytes to Int64s/Floats/Doubles in Haskell

守給你的承諾、 提交于 2019-12-05 12:40:42
I'm trying to parse a binary file format in Haskell (Apple's binary property list format), and one of the things required by the format is to treat sequences of bytes as either (a) unsigned 1-, 2-, or 4-byte integers; (b) signed 8-byte integers; (c) 32-bit float s; and (d) 64-bit double s. Converting sequences of bytes to unsigned integers is easy, and even dealing with signed integers wouldn't be terrible . But for signed integers, and especially for Float s and Double s, I don't really want to implement the logic myself. I've been able to find functions int2Float# :: Int# -> Float# and

Even parity of a unsigned int [duplicate]

天涯浪子 提交于 2019-12-04 16:13:33
This question already has answers here : How to count the number of set bits in a 32-bit integer? (53 answers) Closed 5 years ago . /*A value has even parity if it has an even number of 1 bits. *A value has an odd parity if it has an odd number of 1 bits. *For example, 0110 has even parity, and 1110 has odd parity. *Return 1 iff x has even parity. */ int has_even_parity(unsigned int x) { } I'm not sure where to begin writing this function, I'm thinking that I loop through the value as an array and apply xor operations on them. Would something like the following work? If not, what is the way to

Why does this function count the number of set bits in an integer

情到浓时终转凉″ 提交于 2019-12-04 13:20:45
I was asked the following question in an interview: int foofoo(unsigned int u) { unsigned int foo = u; do{ u = u/2; foo -= u; }while(u > 0); return foo; } I was asked to tell what does this function do and I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? Look at a single bit m inside U . That bit contributes to the value of U like: U = ...... + U m 2^m + ..... (where the ...

intBitsToFloat method in Java VS C#?

China☆狼群 提交于 2019-12-04 06:32:18
问题 I'm getting the wrong number when converting bits to float in C#. Let's use this bit number= 1065324597 In Java , if I want to convert from bits to float I would use intBitsToFloat method int intbits= 1065324597; System.out.println(Float.intBitsToFloat(intbits)); Output: 0.9982942 which the correct output the I want to get in C# However, in C# I used int intbits= 1065324597; Console.WriteLine((float)intbits); Output: 1.065325E+09 Wrong!! My question is how would you convert inbitsToFloat in C

Number of bits to represent a number

自闭症网瘾萝莉.ら 提交于 2019-12-04 02:48:40
I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries. Method 1: function bitSize(num) { return Math.floor( Math.log(num) / Math.log(2) ) + 1; } Pass: bitSize( Math.pow(2, 16) -1 ) = 16 Pass: bitSize( Math.pow(2, 16) ) = 17 Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 Pass: bitSize( Math.pow(2, 48) ) = 49 Method 2: function bitSize(num) { var count = 0; while(num > 0) { num = num >> 1; count++; } return count; } Pass: bitSize(