When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
162 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@weak frost
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
@weak frost Delete this bro or else they will come for your neck!!
Ope.
Post your code dont post screenshots. Trust me!.
I can also drop the cpp file.
Yeah anything but screenshots so delete them now
Yeah but dont forget to delete the screenshots
Is it really that bad an idea? I thought it would be good to show in better detail what I'm talking about with examples of output???
Their name is "zelis" if they catch you , oh man.....
But maybe your screenshots are clearer,imnot a helper maybe im wrong atm but idk yeah nice talking bye.
@weak frost
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
@weak frost put the code back
Hello, I'm a beginner trying to build my own method for taking a number, converting it to a binary array, and then displaying the elements of that array onto the console, the program then allows users to manipulate the bits therein, currently my program works pretty well with only one issue.
Problem
When the Toggle or Negate option is selected, a negative value ends up getting passed into my method that coverts the number to bits and displays it. This causes the wrong result being displayed. I've tried changing the data type relevant variables like bitField, a, and bitValue to types that can store larger numbers, but for some reason the number passed to a is always negative.
The toggle() method should turn the specified bit to a 1 or zero depending on it's current state using XOR, and the negate() method should cahnge every 1 to a 0 and every 0 to a 1 for the current binary array.
Question
My question is how can I address this? I've tried using unsigned long, unsigned long long, and double, but no container seems to be appropriate. This is for a project, and my course encourages independent research, so if someone could explain things rather than just fix it for me, or link some good videos to help with the issue, that would be greatly appreciated.
I've attached some screenshots of the code and the program in various states, and an example of the issue with the negative value shown in my watchlist. Thanks everyone for your time and help.
There, that should be pretty comprehensive.
@weak frost Has your question been resolved? If so, run !solved :)
@weak frost where do you see a negative number?
use unsigned bit if you don't want negatives
a and bitField are negative here, they need to be positive to work properly.
There's a bit data type?
use unsigned values
the problem is that with int, the most significant bit is used as a sign bit
so when you flip it your value "becomes" negative
And to circumvent that I can just change my ints to be unsigned?
yes
Applying that now
I'm not sure that fixed it. I'm using unsigned variables in the toggle method and it generates these crazy garbage numbers.
@weak frost bro you okay?
Why are you inverting isolator here?
void toggleBit()
{
int bit{};
convertToBits(bitField);
std::cout << "\nEnter a number between 1 and 32:" << std::endl;
std::cin >> bit;
int isolator = (1 << bit);
bitField = (~isolator) ^ bitField;
convertToBits(bitField);
std::cout << "\nBit " << bit << " was toggled.";
}
also, shouldn't isolator be the same type as bitField
Because it doesn't return a binary representation that is the same as the original except the specified bit has been toggled. In the case shown here, the output should be
0000 0000 0000 0000 0101 1001 1001 1111.
The toggleBit(int bit) method is a method which shows the user the binary for the number they entered at the beginning of the program, then asks them to enter a number between 1 and 32, this is taken in as the bit to be toggled on or off depending on its current state. an unsigned int called isolater is initialized as 1 left shifted by the value stored in bit, this generates a binary number with 1 in the same bit position as the bit we want to toggle in our bitField,
so if bit = 1 like in the example, then isolator will be a number whos binary representation is
0000 0000 0000 0000 0000 0000 0000 0001
then bitField is assigned as a value equal to (~isolator) ^ bitField.
so in the example shown the operation should look like
1111 1111 1111 1111 1111 1111 1111 1110
XOR 0000 0000 0000 0000 0101 1001 1001 1110
which should be 0000 0000 0000 0000 0101 1001 1001 1111.
This would then be displayed in the console along with a message saying which bit was toggled.
Sorry, it took a while to type this out and verify the code should definitely be working. Hopefully this answers everything.
1 xor 0 = 1
So then I shouldn't be negating isolator.
no
That did it, I was wrong about how XOR worked. The toggle(int bit) method now works as it should.
Did you have any insight about the negate() method? It should literally just do NOT bitField and then print the binary for that to the console.
the negate looks correct
double bitValue = pow(2, i + 1);
I don'T think that's right
The output should be an inverted binary.
I think your conversion or print function is wrong
How so?
why does you conversion function take in bitField as an int
actually, why do you pass it in when you have it as a global variable in the first place....?
lol Because I'm not entirely confident in how to write in C++ yet, so I very well may have just made a few incompetent choices. Please feel free to explain why it doesn't make sense and what I could do better. It'd be leagues above the help I've managed to get thus far.
Same for you. You're both a great help. lol
@weak frost I would avoid the pow and just use bit shift to go over the bitfield
I would also get rid of the "conversion" method and do it in print directly
By the way, if you're on C++20 then you can just do std::format("{:b}", bitField)
I'm not following, the conversion method is necessary for the project.
your conversion method isn't even a conversion method lol
generally I'd expect a function named convert to uh return a value
Like your teacher requires it?
Glad to know my educational investment is paying off so well. So level with me then, walk me through this, because I thought I was onto something and that notion was validated. Like I said, the program starts by asking the user for a number, makes sure it won't break the program by running it through validate(), then is prints the number as binary using the convert and print methods, then a menu that allows the user to manipulate bits. We cannot use bitset and such.
And give me the details, like I'm a moron. Cause I gotta actually learn this, I don't just wanna be fed the answers, yknow?
That's too brief, I need the details. lol
That's really it
And to iterate on the bits
Rather than doing that pow and division
You can do
const bool isSet = number & (1 << x);
Well, if you could explain what I could do instead, and then also why and how it accompishes what I'm trying to do, and maybe even shoot some links my way, that woulld be superb. lol
You just don't need that function
If you want to print then you should call the print function not the comvert function
So put the logic in the print function
I dont know anything about the print function in C++.... They taught us all that using C#....
wat
It's your own print function
You're talking about the printBinary() method I have?
Yes
You're saying just put the logic from the 'convert' method in there and then just nixing the seperate method.
yes
You guys are awesome. Okay that makes sense.
Yeah man, don't get me started. This is a bigger name college and I've just been lost the entire program. Got damn near a 4.0, but I'll be damned if I can get a teacher to sit down and explain this stuff to me. That GPA is me moving the wieght all by myself. lmao.
I called the advocacy team and even they said pretty much the same thing you did.

@weak frost you're doing a great job trying to learn stuff
Keep on like that and continue asking for help when you're getting stucked
Thanks! I got rid of the unnecessary method, but I still have the negate that isn't outputting what I would expect, that being the inverted version of the binary representation. If its a 0 it becomes a 1, and 1becomes 0 for the whole bitfield. You said some stuff about there being an iisue with the logic in how I print out the binary and something else, could we go back to that?
could you show what your print function looks like now?
Let me know if you need me to do anything else.
@weak frost
Take this for example
void printBinary()
{
const int numBits = sizeof(bitField) * 8;
for (int i = numBits - 1; i >= 0; --i)
{
auto bit = (bitField >> i) & 1;
std::cout << bit;
}
}
Instead of pow, just shift
I have no idea if this actually works since i didn't test it lol but it should convey the concept
Hmmm, you might have me lost again. The way they're teaching us, when you convert a number to binary, you're essentially dividing the number by a power of two, if the number can fit, its a 1 and you subtract the power of 2 value if not then its 0 and you move on to the next bit until you get to 2to the 0, then you have a completed binary conversion.
Eh... that sounds kind of overcomplicated to me. I mean, it sounds like it'd work, but why do all that when you can directly bit-mask the bit you want?
Again, don't get me started with how these guys are going about things. THat's that way the said to convert so that's the only logic I had to draw on when I made the method. They didn't eplain how to to any of what you just did or how it works. lol
oof
I was told my code looked great after asking for help, it still didn't even work properly.
So what are you doing here in this example you sent me? can you walk me through it?
It's basically just (bitField >> i) & 1 that's important here
it shifts bitField right by i spots, and then takes the lowest bit - which was the ith bit of bitField
it just loops over all the bits from highest to lowest (since we want to print the highest bit first)
Can you explain how it does that a little more? I've really been struggling to internalize and visualize how all the shifting and bitwise operators work.
Ugh, I keep forgetting to hit reply. sorry.
Lets say you have the bits 0100 and you want the 2nd bit from the right (0 indexed)
shift right by 2 bits: 0100 >> 2 -> 0001
take the last bit: 0001 & 1 -> 1
Okay, I think I can see how that works. This moves the bit at the last index to the first, and then ands it with 1, which means 1and1 will be 1, 1and0 will be 0 and then outputs it to the stream. So just loop over the int array that I populat unsing the logic from the appropriated convert method? Or are we saying ditch most of the logic here and this can cover all that?
Just ditch it, yeah
there's no real point in filling an array just to print it when you can just print each item directly
yup!
I'm getting it!
Looking good, but when I did that I lost some of the formatting, know how I can reincorporate the bit I had where it grouped them into 4s? Could I just do the if but with i for the modulo or something?
Yup
You still on?
I am
(though obligatory don't ask to ask, just leave a message lol)
Think you could help me with something else real quick? I'm trying to make a catch for my turn on and turnoff methods that checks to see if the bit in question is already on or off respectively, before going in and performing an operation. The Turn on method works perfectly, but I'm having issues with my turn off method. Do you think you could help me understand why? I feel like XOR is the right check to make, but I'm expecting the value of bit to be a 1 or a 0 and it's not, instead it's a large int.
Heard. lol
why do you think that XOR is the right check to make?
Because if bitEval equals 0 I want it to turn on the bit, and if bitEval equals 1 I want it to say the bit was already off. I looked at a truth table and the only operator that fits is XOR because 0^1 = 1 and 1^1 = 0.
Right?
You're not working at the level of individual bits though, you're xoring with the whole bitField
Let's say bitField is 1101, and we're checking the second bit. With your current method, what happens step by step?
Let me rearrange your code a bit:
// Let bitField = 0b1101, index = 2
int isolator = 1 << index - 1; // what is isolator?
int bitEval = bitField ^ isolator; // what are the two operands to the xor?
1101 ^ 0010?
yup
exactly
Which is very wrong.
So instead I should be using & still? Or should I be negating somewhere maybe?
Let me answer with a question, how does your turnOnBit function do its check?
specifically, what does bitEval mean in that function?
If bitEval == 0 that means the bit was on and needs to be shut off, so I do bitField = bitField & (~isolator);. If bitEval == 1 then the bit is already turned off and I just out a message saying that.
I need to remeber to hit reply. 🤦
I was thinking the operation I have to assign a value to bitEval would esentially take the current bitField and then do a bitwise operation on it against a number = to 1 leftshifted by index-1. Then that value will be either 1 or 0 and get's passed to the if statement.
Like.
bitfield: 1101
index: 2
(1 << index -1) 0010
1111
0000
1101
| 0010 |
|---|
| 1111 |
So I'm at a loss. lol
But as you've figured out, that method doesn't work, since that xor doesn't give 1 or 0
What does your turnOnBit function do?
If the specified bit is a 0, it turns it to a 1 and turns it on, if it's already 1, is says it's already on. Very similar to TurnOff.
Could I use & and then just siwtch the conditions of the if statements?
Maybe 👀
Gonna test this.
The thing I wanted to allude to is that at a high level, the turnBitOn function goes something like this:
It works!
You really helped me understand bit manipulation more in depth! I really appreciate your time!
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
So I know you're probably trolling @eager coyote but let's set the record straight for the new person on the server (and welcome @weak frost).
It is in our #rules that screenshots and photos especially of code, error messages, etc. should not be sent. Don't be lazy, just copy-paste. The bot tells people about this. You were warned because you repeatedly asked the same exact question with the same exact photos (the worst photos I've ever seen, just for the record) and were repeatedly told to stop sending the photos.
Ope...
@weak frost tbh I found you were great at describing your problem and trying to add details
ditto
this here is the gold standard of what all questions should look like
They're pretty stringent about that at my school. Lol Real sticklers for all the traditional conventions.