bits

Bit operations (C++)

老子叫甜甜 提交于 2019-12-02 21:05:37
Recently I had a question on the interview - I was asked to compare bitwise operations in performance terms. Like, give a brief description of the performance of different bit operations. I guess this question could is pretty general and pretty machine-specific, but I also think there should be some general rules about this, which you have to mention (and I didn't :). So - what would you answer? I probably should also say, that it may be a good idea to compare their performance in C (or C++, whatever) , because I assume that these languages give more space for the compiler to perform bit

Is there a way to access individual bits with a union?

孤者浪人 提交于 2019-12-02 19:18:30
I am writing a C program. I want a variable that I can access as a char but I can also access the specific bits of. I was thinking I could use a union like this... typedef union { unsigned char status; bit bits[8]; }DeviceStatus; but the compiler doesn't like this. Apparently you can't use bits in a structure. So what can I do instead? Sure, but you actually want to use a struct to define the bits like this typedef union { struct { unsigned char bit1 : 1; unsigned char bit2 : 1; unsigned char bit3 : 1; unsigned char bit4 : 1; unsigned char bit5 : 1; unsigned char bit6 : 1; unsigned char bit7 :

Grabbing n bits from a byte

痴心易碎 提交于 2019-12-02 17:21:54
I'm having a little trouble grabbing n bits from a byte. I have an unsigned integer. Let's say our number in hex is 0x2A, which is 42 in decimal. In binary it looks like this: 0010 1010. How would I grab the first 5 bits which are 00101 and the next 3 bits which are 010, and place them into separate integers? If anyone could help me that would be great! I know how to extract from one byte which is to simply do int x = (number >> (8*n)) & 0xff // n being the # byte which I saw on another post on stack overflow, but I wasn't sure on how to get separate bits out of the byte. If anyone could help

Multiplication of two 16-bit numbers - Why is the result 32-bit long? [closed]

∥☆過路亽.° 提交于 2019-12-02 10:03:01
If I multiplie two 16-bit numbers, the result will be 32-bit long. But why is this so? What is the clear explanation for this? And for my right understanding: The calculation for this is: n-bit number multiplied with a m-bit number gives a (n+m) bit number? (2 n - 1)*(2 m - 1) = 2 n+m - 2 n - 2 m + 1 -(2 n + 2 m ) is like clearing the bits at index n and m , which does not affect much the result compared to 2 n+m , so you need n+m bits to represent the result. For example 1111 2 *1111 2 = 11100001 2 (15*15 = 225) In general, (b n - 1)*(b m - 1) = b n+m - b n - b m + 1 , so multiply an n-digit

Find most significant set bit in a long

放肆的年华 提交于 2019-12-02 08:19:42
I'm in the unique situation where searching for "most significant bit" yields too many results and I can't find an answer that fits my needs! The question itself is pretty simple: "How do I find the most significant set bit in an unsigned long?" When I do my calculations the rightmost bit position is position '0'. I know that it involves masking the lowest bit, checking and then shifting left to once while incrementing my count, and then repeating with the 2nd lowest, etc. I've done this before but for whatever reason I can't do it now. Edit: By "most significant" I mean leftmost set bit,

intBitsToFloat method in Java VS C#?

倖福魔咒の 提交于 2019-12-02 08:15:16
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#? My attempt: I looked to the documentation here http://msdn.microsoft.com/en-us/library/aa987800(v=vs

C# - Shifting and reversing the order of bits in a byte array

十年热恋 提交于 2019-12-02 08:09:06
I am trying to get the correct int out of an array of bytes. The bytes is read from a RFIDTag via POS for .Net. (Acctually I need 18 bits) In binary the byte array is as follows: 00001110 11011100 00000000 00011011 10000000 What I need to get out of it is: 00 00000000 11101101 (int = 237) From the original bytes that would be the following bits in reverse order: ------10 11011100 00000000 I have been looking at bitArray. Array.Reverse. And several ways of shifting bits. But I just can't wrap my head around this one. Can anyone point me in the correct direction? You can get the bits and reverse

Can i access TBits internal bitmap?

此生再无相见时 提交于 2019-12-02 07:16:17
问题 In particular, i want to preset desired size, fetch a bitmap from external source, and then work with data in classy objecty-oriented manner. I presume what TBits isnt just a straightforward collection of Booleans and internal storage is contiguous. Am i correct with such assumptions? 回答1: Correct, TBits is internally bit-structured, so it's not a straightforward collection of booleans. Yes, storage is handled by allocating contiguous memory big enough to carry the size( in increments of

print bits of a void pointer

旧城冷巷雨未停 提交于 2019-12-02 06:57:47
If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated? For example: void * p; p = malloc(24); printf("0x%x\n", (int *)p); I would like the above to print the 24 bits that I just allocated. size_t size = 24; void *p = malloc(size); for (int i = 0; i < size; i++) { printf("%02x", ((unsigned char *) p) [i]); } Of course it invokes undefined behavior (the value of an object allocated by malloc has an indeterminate value). You can't - reading those bytes before initializing their contents leads to undefined

write individual bits to a file in python

狂风中的少年 提交于 2019-12-02 04:23:29
is there a way in python to write less than 1 byte data even when I write the number 0 which represented in 1 bit the file size is 1(8 bits) byte I tried the struct module file.write(struct.pack('b',0)) array module import array data1=array.array('B') x=bin(0)[2:] data1.append(int(0,2)) f2=open('/root/x.txt','wb') data1.tofile(f2) Ryan Haining No you cannot write less than a byte. A byte is an indivisble amount of memory the computer can handle. The hardware is not equipped to handle units of data <1 byte (though the size of a byte may differ from machine to machine). The file system also