A list of puns related to "Bitwise operation"
I need to work with large integers and the standard 64 bit uint doesn't cut it. Using 2 of these and doing bitwise operations is easy enough, but I can't seem to figure out how to tell if my carry caused the number to overflow.
I have an enum:
enum mode {
WALK = 1,
JUMP = 2,
WALK_JUMP = 3,
RUN = 4,
RUN_JUMP = 6
};
Is there a way to write the below shorter using bitwise operators?
if (mode == JUMP || mode == WALK_JUMP || mode == RUN_JUMP) {
//
}
Edit: Fixed errors in the code
reg [15:0] maskarr[0:7];
wire[15:0] mask;
would be something like
for each column, i, in maskarray:
bitwise OR all the elements in the column and put the result in mask[i]
I found the syntax
reg [15:0] maskarr;
wire mask = |maskarr;
but this doesn't seem to be able to be expanded to a 2d bitarray like so:
reg [15:0] maskarr[0:7];
wire[15:0] mask = |maskarr;
I can do
always
for (integer i = 0; i < 16; i=i+1)
mask[i] = |(maskarr[i]);
but I'm wondering if there's an easier cleaner way so that I do not need a for-loop.
If I write:
unsigned int var = 10;
Serial.println(~var);
On my ESP32, it converts the number into an unsigned long. If I use uint16_t it treats it as a signed int.
My solution was to cast it which worked.
Serial.println(uint16_t(~var));
This doesn't seem like intended behavior. Is there a place I can submit a bug report? And to who?
It was XOR-bit-ant-ly priced.
so i have two values
95402AF9 and 6ABFD506
i can convert 6ABFD506 to 2504010489 to doing the following
crc= int("6ABFD506",16) ^ 0xFFFFFFFF
then
hex(crc) == 0x95402AF9
how do i do this the other way IE convert 95402AF9 into 6ABFD506
thanks
#include<stdio.h>
int main()
{
int i= -15;
printf("%d\n",i>>1);
printf("%d\n",i>>2);
printf("%d\n",i>>3);
printf("%d\n",i>>4);
printf("%d\n",i>>5);
return 0;
}
I got asked by my friend as how exactly does the Bitwise shift operator work on negetive numbers? As to how the MSB/Sign bit is preserved?
I am supposed to determine the bit mask and shift values based on the following 3 values: the start byte, the start bit, and the number of bits.
I will receive some byte array for my data of x many bytes. Let's say some random chunk of data within this large byte array represents whether a light switch is on or off. So I will focus on this single byte/8-bits of data for this example. 3 bits out of these 8 bits are used to determine the on/off state of the light switch.
So, all I know about this chunk of data are these 3 fixed values: the starting byte position within this byte array, the starting bit position within this specific byte itself, and the number of bits allocated to represent the on/off state of this light switch.
So in this particular example, the byte position is byte 15 (within this large byte array of x many bytes). The starting bit position is 0. The number of bits is 3 (since 3 bits are allocated within this byte to represent on/off state).
My question is: how do I calculate the bit mask and bit shift value based on these 3 fixed values? I know the calculation might involve base 2 or raising to the power of 2, but unsure.
Thanks in advance
I have 0 knowledge of bitwise operations and I want to master it. I am looking for some resourcesπ to learnπ©βπ» bitwise operations form Beginner to Advanced.
Hi All,
can anyone recommend some good resources on Bitwise operations/Bitwise Manipulation/ Bit Hacks in Java Please
thanks
Do you think it's a good idea to use the same operator for logical and bitwise operations?
1 & 5 -> 1 (bitwise)
true & false -> false (logical)
The same for | (or) ^ ( xor).
It sort of converges when using vectors.
{true, false, false} & {true, true, false} -> {true, false, false}
It only works if the language is strictly typed. Anything else I may miss?
Little nitpick is, ^ is now used, so power would need to be pow() or **.
Are there languages such syntax?
Edit: Thanks for the great inputs. It seems short-circuiting is the biggest concern. Functions in my language have no side effects. So I think it is less of an issue. (Short-circuit is in fact an optimization pass). My language has no pointers so the common "test for nullptr and dereference" does only seldom happen with optionals. Then it's just a sensible left to right evaluation.
More of a CPT coding pro tip hehe
So, I came across a bitwise question where given integers n, p1 and p2, determine if the bits of position p1 and p2 are the same. Positions p1, p2 are 1 based with 1 being the least significant bit.
The concept of bitwise operation is new to me. So please correct me if I am wrong as I am still trying to get my head around this.
So firstly, when talking about least significant bit, it means reading bits from right to left, correct? Next, when talking about 1 based, it means starting count from 1 rather than 0, correct?
So, let's say n is 86, p1 is 2 and p2 is 3.
As 86 in binary number is 1010110, both p1 and p2 are the same, correct? Counting from left to right, the bit at their position is both 1.
Coding this:
I came across a solution for this to be bool same_bits = (((n >> p1-1) & 1) == ((n >> p2-1) & 1))
. Now I understand the concept but not quite sure how it works. I get that I am comparing p1 bit and p2 bit but don't get how it's obtaining those bit through that code.
Could someone please explain this code to me?
As the title states, is it generally more efficient to use bitwise operations rather than conditional statements/operations (assuming both are optimized)?
As an example, I'm currently writing some code (in Java), and for a particular section, I want a value to be greater than or equal to 0 (if the value is negative, then treat it as 0).
Assuming n
and y
are both signed 32-bit integers, the following statements will complete the same task:
1.) y = (n < 0 ? 0 : n);
2.) y = n & ~(n >> 31);
Ignoring ease of readability, would statement 1 or 2 be better?
To account for XY problem the X question is as follows:
This is a task in structs and unions topic.
Write functions that do bit operations on 32-bit unsigned integer.
int toggle_bit(uint32_t* pvalue, int b);
int set_bit(uint32_t* pvalue, int b);
int clear_bit(uint32_t* pvalue, int b);
int isset_bit(const uint32_t* pvalue, int b);
Functions are to take pvalue pointer to a number and a bit number. They should return -1 in case of incorrect input and 0 on success with exception of function isset_bit which should return 0 or 1 if given bit is set or not. You may not use bit-wise operators, allocate memory or use [] operator except array declarations. Write a program that takes number, bit number and operation number (0 - toggle, 1 - set, 2 - clear, 3 - check if set) and then prints edited number in hexadecimal format (for operation 0-2) or 0/1 in decimal (for operation 3).
The Y question is:
I have written this functions that I think do work but I am really not satisfied with the solution. Pasting only toggle_bit function because the rest is very similar.
union bits_set {
uint8_t bit_val;
struct {
uint8_t bit0 : 1;
uint8_t bit1 : 1;
uint8_t bit2 : 1;
uint8_t bit3 : 1;
uint8_t bit4 : 1;
uint8_t bit5 : 1;
uint8_t bit6 : 1;
uint8_t bit7 : 1;
} bits;
};
union bytes_set {
uint32_t value;
union bits_set bytes[4];
};
int toggle_bit(uint32_t* pvalue, int b)
{
if (pvalue == NULL || b < 0 || b > 31) return -1;
union bytes_set bytes;
bytes.value = *pvalue;
union bits_set *dst_byte = (bytes.bytes + b / 8);
uint8_t dst_bit = b % 8;
if (dst_bit == 0) dst_byte->bits.bit0 = (dst_byte->bits.bit0 + 1) % 2;
else if (dst_bit == 1) dst_byte->bits.bit1 = (dst_byte->bits.bit1 + 1) % 2;
else if (dst_bit == 2) dst_byte->bits.bit2 = (dst_byte->bits.bit2 + 1) % 2;
else if (dst_bit == 3) dst_byte->bits.bit3 = (dst_byte->bits.bit3 + 1) % 2;
else if (dst_bit == 4) dst_byte->bits.bit4 = (dst_byte->bits.bit4 + 1) % 2;
else if (dst_bit == 5) dst_byte->bits.bit5 = (dst_byte->bits.bit5 + 1) % 2;
else if (dst_bit == 6) dst_byte->bits.bit6 = (dst_byte->bits.bit6 + 1) % 2;
else if (dst_bit == 7) dst_byte->bits.bit7 = (dst_
... keep reading on reddit β‘In C,
Bitwise logical operators &
, |
, ^
is used for selecting bits in a word.
Bitwise shifting operators >>
and <<
can be used for implementing multiplication and division between integers.
Questions:
Are bitwise logical operators also used for implementing operations on values of higher types (e.g. integers)?
Are bitwise shifting operators also used for bits operations?
Thanks.
https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/
enum test{
test_value_one = 1 << 0
};
int l_flags = 0;
// set flag
l_flags |= test_value_one;
// unset
l_flags &= ~test_value_one;
// we can check if flag is set by
if(l_flags & test_value_one){
/// flag is set
}
// or if not set
if(!(l_flags & test_value_one)){
/// flag not set
}
//// is there are a other way to check if flag is not set?
/// why we can not do like &~ (AND NOT), or do we ? somehow I thing it will be faster then !( &)
if(l_flags &~ test_value_one){
/// flag not set ?
}
Documentation and questions seem sparse
I am attempting something like this in gdscript:
# flags
const FLAG_NONE = 1 << 0
const FLAG_A= 1 << 1
const FLAG_B = 1 << 2
static func flag_is_enabled(b, flag):
return b & flag != 0
static func set_flag(b, flag):
return b|flag
#static func unset_flag(b, flag):
# return b&^flag # Error parsing expression, misplaced: '^'
This is one little thing driving me nuts because I'm not fully comfortable with bitwise operations to begin with and attempting to do this in gdscript while learning gdscript is frustrating. Is this correct? How to do &^ without causing an error? Any suggestions?
EDIT:
this appears functional
# flags
const FLAG_NONE = 1 << 0
const FLAG_A= 1 << 1
const FLAG_B = 1 << 2
static func flag_is_enabled(b, flag):
return b & flag != 0
static func set_flag(b, flag):
b = b|flag
return b
static func unset_flag(b, flag):
b = b & ~flag
return b
kind of a pain but part of the learning process
I was looking at the intel intrinsics guide, and I found a intrinsic for an instruction that takes in 3 zmm registers as input, and a imm8 that serves as a truth table. The instruction uses the truth table to perform any 3-bit operation that you want. For the ymm and zmm versions, the latency is reported as 1, and the reciprocal throughput is reported as 0.5. For the xmm version, the reciprocal throughput is lower at 0.33.
Intrinsic: _mm512_mask_ternarylogic_epi32
Instruction: vpternlogd zmm {k}, zmm, zmm, imm8
CPUID Flags: AVX512F
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#techs=AVX_512&cats=Logical&expand=5865
I'm a beginner programmer and I've been learning python on my own and also C as part of my college curriculum. I have stumbled upon Bitwise operators on both of the languages while reading and i understand how they work and all but I can't understand their purpose, how are they used in real life programs, and what do I benefit from operating on a bit level ?
Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.