A list of puns related to "Bitwise Operator"
My computer text book is from APC(class 10 only since school said that the first 9 chapters are the same in grade 9), but it seems to lack some stuff from grade 9 and I'm confused since there are no bitwise operators or interfaces mentioned and I saw questions related to them in a few sample papers. Idk if they're actually needed.
If they are can anyone also send some PDFs or images about the mentioned topics
I read a few articles and kind of understood the basics of how the bitwise operator & works.
But what I don't understand is why we have been asked to use (buffer[3] & 0xf0) == 0xe0 when instead we can just use (buffer[3] & 0xe0) == 0xe0 ?
Can't seem to wrap my head around it :(
Here is my understanding of how the bitwise and operator is being used in the draw functions (corrections or clarifications welcome!)
Using the and bitwise operator (&=) will ensure that boolean value will remain 'false' if ever it is compared with a false value. ie It will be 'true' until compared with a 'false', whereupon it will be 'false' ever after.
Once a line or shape is drawn outside the screen it is now forever 'uncontained' by the screen. ie contained = 'false' ever after.
I probably could have picked a more succinct title. I'm reading the book Rust in Action and am enjoying it thus far. Having come from dynamically typed languages, it's been a really interesting read! There's an example (Listing 5.9) in the book that extracts the fields of a float (sign, exponent, and mantissa).
The part I'm curious is why the "AND mask" operations are being used. The following code is from the function from listing 5.9 (I added comments and line numbers for convenience):
21| let bits = n.to_bits(); // bits == 1000010001010011010111000010100
23| let sign = (bits >> 31) & 1;
// bits >> 31 as binary == 0, 0 & 1 == 0
// sign as binary == 0
24| let exponent = (bits >> 23) & 0xff;
// bits >> 23 as binary == 10000100, 0xff as binary == 11111111
// exponent as binary == 10000100
25| let fraction = bits & 0x7fffff;
// bits (binary) == 1000010001010011010111000010100
// 0x7fffff == 0000000011111111111111111111111
// fraction as binary == 1010011010111000010100
I get that for the Mantissa, the goal is to strip away the signed bit and the bits representing the exponents (hence why 0x7fffff's length is shorter than bits' length). But why do we need to use the "AND mask" for lines 23 and 24 if they end up evaluating to the same thing?
Thank you in advance!
For those who don't know, bitwise operators are operators for manipulating bits in your program.
https://en.wikipedia.org/wiki/Bitwise_operation
They are cool, but when it would be useful in a game project?
Hi everyone! I am comfortable with basic questions that use bitwise operators to solve questions like duplicate number, counting number of bits set in a number.. Etc
But i am not really able to wrap my head around these concepts for multiple other problems like finding subsets etc. The usage of bitwise operators is not at all intuitive to me.
Could someone pls suggest what needs to be done to get comfortable with these to start identifying and applying them by myself?
I was reading on /r/python about the snippet of code that creates a 32Tb pyc file and someone asked what << does.
The reply was very dense and in my experience bitwise operator explanations are always very complex.
Is there an ELI5 version that will help me understand them?
Can you show me some examples of when bitwise operators are useful?
For examples I mean code. I still don't got it. I've passed my entire afternoon searching examples on the internet but I found nothing useful and clear
Could someone explain me the logical reasoning of why bitwise Operators are efficient in Big O?
Like as an example program of checkBit(int a, int b), where is an integer input number and b represents the bit position of a in binary, so if its 1, it just returns 1 or true something like that... if its done using bitwise operator, why does its time complexity better with just single statement like ==> (a >> b)%2 or (a>>b) & 1 or (a << b) & a ??
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.
Suppose I have:
int oddOrNot = 0;
oddOrNot = 5 & 1;
How is the "&" being used to check for oddity? I know & represents the AND logical operation, but I'm unsure what that has anything to do with oddity and how that would even work...
For example, I have 01100010 stored in an unsigned int, and I want to change the LSB, here it is 0, to 1. Just that, and nothing else. How would I go about doing this?
Also, let's say in a different case, if the LSB is 1, and I want to change it to 0, would the code be the same?
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 ➡I have a binary number and I want to rotate the bits from left to right 1 bit at a time.
eg: 1 0 1 -> 0 1 1 ->1 1 0- > 1 0 1
other sites like geeks for geeks use (n << 1) | (n >> (INT_BITS - 1)). This gives a wrong answer for me.
The geeks for geeks tutorial says when we left shift by one bit, the first bit just disappears. but in my python interpreter the number is multiplied by 2.
geeks for geeks: 1 0 1 -> 0 1 0
my python interpreter: 1 0 1 -> 1 0 1 0
Please tell me the working code for rotating the bits
Thanks in advance.
I have a code snippet which I don't fully understand. I tried walking through the code with the debugger, but I still don't get it.
This is taken from the Android SDK of Godex, a printer manufacturer. It is part of a decompiled class, so this might be why the syntax is so interesting?
This part of the code is supposed to send an image to the printer. It is part of a bigger function, but I just added the relevant part. I don't understand the use of constructedByte.
To me the | operator (bitwise OR) just increase the variable by 1 and the << (bitwise shift to the left) just doubles it. What does one end up with in data at the end? So what is the byte object constructedByte
supposed to represent in the end?
int height = 100;
int weidth = 100;
ArrayList<Byte> data = new ArrayList();
String qcommand = "Q" + px + "," + py + "," + y + "," + height + "\n";
byte[] startingBytes = qcommand.getBytes();
int y;
for(y = 0; y < startingBytes.length; ++y) {
data.add(startingBytes[y]);
}
int x;
for(y = 0; y < height; ++y) {
for(x = 0; x < y; ++x) {
byte constructedByte = 0;
for(int j = 0; j < 8; ++j) {
constructedByte = (byte)(constructedByte << 1);
int widthPixel = x * 8 + j;
if (widthPixel < width) {
int pixel = pixels[y * width + widthPixel];
if (pixelBrightness(pixel) < 127) {
constructedByte = (byte)(constructedByte | 1);
}
}
}
data.add(constructedByte);
}
}
I have been trying to understand how actually binary addition using bitwise operators AND, and XOR, work and I think I finally understand it, after spending almost a day thinking about it :P What confused me actually was when I tried creating the algorithm in my head, is this correct? thoughts
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.