[Solved] What is Byte Value for Boolean

Just getting started with rust, and as I was reading the documentation it came to Booleans are represented with a single Byte. I wanted to know what are the byte values that are being used for True and False. Some Language that I have worked with in the past used for False and X for True, I guess others could use 0 for true, another way would be to T and F.

Example if T was used => |01010100|

I don't know where and why I would ever need it but just out of curiosity I would like to know.

Bonus points if you can help me find it via code. I am an absolute beginner when it comes to rust, so I tried to convert the boolean to a string or character but was not able to do so, tried looking up what methods can be applied to a boolean and there isn't much. I was able to use to_string(), but it came back true or false.

A false is equivalent to all zeros 00000000. A true is equivalent to 00000001. You can see it like this:

use std::mem::transmute;

fn main() {
    unsafe {
        println!("{}", transmute::<bool, u8>(true));
        println!("{}", transmute::<bool, u8>(false));
    }
}
1
0

playground

1 Like

Because a bool is not a u8 I would say "A false is represented in memory by all zeros, 00000000; a true is represented in memory by 00000001 ."

How are u8s stored? Do they follow unicode standards, or because they are not a char or string; rust does not follow those standards when storing it?

ie
x: u8 = 1
x byte value is 00000001 or is it 00110001 <== UTF-8 Binary for '1'

might be a little off topic but, because we went form bool to u8 just curious

going further 2 is 00000010; Which would make sense since u8 can go up to 256 which is 2^8

Numeric types in Rust aren't stored as strings, there's no need for that and it would be unnecessarily complicated and inefficient. They are stored as their "raw" memory representation. An u8 is a single byte, a u16 is stored on 2 bytes, etc.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.