bit-manipulation

How can I create a 48-bit uint for bit mask

早过忘川 提交于 2019-12-04 20:50:23
I am trying to create a 48-bit integer value. I understand it may be possible to use a char array or struct, but I want to be able to do bit masking/manipulation and I'm not sure how that can be done. Currently the program uses a 16-bit uint and I need to change it to 48. It is a bytecode interpreter and I want to expand the memory addressing to 4GB. I could just use 64-bit, but that would waste a lot of space. Here is a sample of the code: unsigned int program[] = { 0x1064, 0x11C8, 0x2201, 0x0000 }; void decode( ) { instrNum = (program[i] & 0xF000) >> 12; //the instruction reg1 = (program[i]

Methods to form and check bitmasks

亡梦爱人 提交于 2019-12-04 20:33:12
This most likely has been asked and answered before, but my searches was futile. Question is about bits, bytes masks and checking. Say one have two "triggers" 0xC4 and 0xC5 : 196: 1100 0100 0xc4 197: 1100 0101 0xc5 The simple way of checking if var is either would be: if (var == 0xc5 || var == 0xc4) { } But sometimes one see this (or the like): if ( ((var ^ magic) & mask) == 0) { } My question is how to find magic and mask . What methods, procedures, tricks etc. is to be utilized to form these values and to assert if any exists? EDIT: To clarify. Yes, in this exact example the former would be

Obtaining bit representation of a float in C

故事扮演 提交于 2019-12-04 19:47:04
I'm trying to use unions to obtain the bit representation of float values, my code is currently as follows: union ufloat { float f; unsigned u; }; int main( ) { union ufloat u1; u1.f = 3.14159f; printf("u1.u : %f\n", u1.u); However anything I try to print gets printed as 0.0000000, instead of as bits (such as 0001 0110, or something similar), what is wrong in my code? Note that preferebly I would like to use unions to achieve this. There are a large number of ways to accomplish this. Understand that what you are really trying to do is simply output the bits in memory that make up a float .

c: bit reversal logic

前提是你 提交于 2019-12-04 19:41:18
I was looking at the below bit reversal code and just wondering how does one come up with these kind of things. (source : http://www.cl.cam.ac.uk/~am21/hakmemc.html ) /* reverse 8 bits (Schroeppel) */ unsigned reverse_8bits(unsigned41 a) { return ((a * 0x000202020202) /* 5 copies in 40 bits */ & 0x010884422010) /* where bits coincide with reverse repeated base 2^10 */ /* PDP-10: 041(6 bits):020420420020(35 bits) */ % 1023; /* casting out 2^10 - 1's */ } Can someone explain what does comment "where bits coincide with reverse repeated base 2^10" mean? Also how does "%1023" pull out the relevent

More idiomatic way in Go to encode a []byte slice int an int64?

我是研究僧i 提交于 2019-12-04 19:14:14
问题 Is there a better or more idiomatic way in Go to encode a []byte slice into an int64? package main import "fmt" func main() { var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244} var data int64 for i := 0; i < 8; i++ { data |= int64(mySlice[i] & byte(255)) << uint((8*8)-((i+1)*8)) } fmt.Println(data) } http://play.golang.org/p/VjaqeFkgBX 回答1: You can use encoding/binary's ByteOrder to do this for 16, 32, 64 bit types Play package main import "fmt" import "encoding/binary" func main()

How to write a constant time function to copy the most significant bit to all bits

Deadly 提交于 2019-12-04 19:12:30
问题 I'd like to write a function, in C, which takes the MSB of uint8_t , and if it's set, returns 0xFF and if not 0x00 . In short, which returns an integer where all the bits are set to the same value as the MSB. But I'd like to do it in a completely constant time way, no branches, no array offsets, just mathematical operations which are guaranteed to always touch the same number of bits. And ideally, without any undefined behavior. How can this be done? 回答1: How about: #define uint8_msb_to_all

Are there any good reasons to use bit shifting except for quick math?

倖福魔咒の 提交于 2019-12-04 18:55:04
问题 I understand bitwise operations and how they might be useful for different purposes, e.g. permissions. However, I don't seem to understand what use the bit shift operators are. I understand how they work, but I can't think of any scenarios where I might want to use them unless I want to do some really quick multiplication or division. Are there any other reasons to use bit-shifting? 回答1: There are many reasons, here are some: Let's say you represent a black and white image as a sequence of

Bit hack: Expanding bits

微笑、不失礼 提交于 2019-12-04 17:44:28
问题 I am trying to convert a uint16_t input to a uint32_t bit mask. One bit in the input toggles two bits in the output bit mask. Here is an example converting a 4-bit input to an 8-bit bit mask: Input Output ABCDb -> AABB CCDDb A,B,C,D are individual bits Example outputs: 0000b -> 0000 0000b 0001b -> 0000 0011b 0010b -> 0000 1100b 0011b -> 0000 1111b .... 1100b -> 1111 0000b 1101b -> 1111 0011b 1110b -> 1111 1100b 1111b -> 1111 1111b Is there a bithack-y way to achieve this behavior? 回答1:

Create method which checks if x + y will overflow using bitwise operations

巧了我就是萌 提交于 2019-12-04 17:12:21
I need to create a method in C using bitwise operations which checks if x + y will overflow or not. I can only use a maximum of 20 of the following operations; ! ~ & ^ | + << >> Keep in mind I have to test for both negative and positive numbers. I've tried several times to make it work. Is my logic sound? I'm going by: if (x + y) is less than x, then it has overflowed. Based on that logic, I wrote this; int addOK(int x, int y) { int sum = x + y; int nx = ((~x) + 1); int check = (sum + nx)>>31; return !check; } Thank you! This should work, but it doesn't use only bitwise operator, but it work

re implement modulo using bit shifts?

时光毁灭记忆、已成空白 提交于 2019-12-04 17:00:18
问题 I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my code, as of now one cycle of my mainloop does not run in 1/60 of a second as it should. I was wondering if it was possible to re-implement the modulo using only bit shifts like is possible with multiplication and division. So here is my code so far in